에러

    [파이썬] TypeError: list indices must be integers or slices, not str

    x 리스트에서 조건에 맞는 값을 y리스트에 추가 후 출력하려고 하는데 아래와 같은 오류가 발생한다 x = ["grapes", "orange", "apple", "lime", "banana", "cherry", "kiwi", "blueberry", "watermelon"] y = [] for i in x: if (i == "apple") or (i == "kiwi"): y.append(x[i]) print(y) --------------------------------------------------------------------------- y.append(x[i]) ~^^^ TypeError: list indices must be integers or slices, not str TypeError: ..

    [파이썬] TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

    두 개의 정수값을 입력받으려고 input(""). split()으로 문자열을 받고 이 것을 int()로 묶어서 형변환을 시키려 했더니 num, num2 = int(input("").split()) >> 2 10 TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' 타입에러가 발생한다. 해석해 보니 라고 한다. 그 말인즉슨 input("").split()으로 입력한 문자를 나눈 순간 각각의 문자열로 적용되는 것이 아닌 리스트에 담긴다는 의미이다. 해결 방법으로는 2가지가 있는데 첫번째로 문자열로 입력받고 하나씩 형변환 시키는 방법 num, num2 = input("").split() num = int(n..

    [파이썬] TypeError : unsupported operand type(s) for

    n = int(input("입력: ")) for cnt in range(n): max += cnt print(max) /// TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int' 서로 연산이 불가능한 타입끼리 연산을 시도할 때 나타나는 에러. max+=cnt 부분을 보면 cnt변수는 int형이지만 max변수는 builtin_function_or_method 라는 타입이다. 반복문 들어가기 전에 max=0이라 적어서 int형으로 만들어주면 해결된다.

반응형