본문 바로가기
파이썬

파이썬 +에 대해 지원되지 않는 피연산자 유형 : 'int'및 'str'

by º기록 2020. 12. 27.
반응형

저는 현재 파이썬을 배우고 있으므로 무슨 일이 일어나고 있는지 전혀 모릅니다.

num1 = int(input("What is your first number? "))
num2 = int(input("What is your second number? "))
num3 = int(input("What is your third number? "))
numlist = [num1, num2, num3]
print(numlist)
print("Now I will remove the 3rd number")
print(numlist.pop(2) + " has been removed")
print("The list now looks like " + str(numlist))

프로그램을 실행하여 num1, num2 및 num3에 숫자를 입력하면 다음과 같이 반환됩니다. 역 추적 (가장 최근 호출 마지막) :

TypeError: unsupported operand type(s) for +: 'int' and 'str'

 

해결 방법

 

잘못된 문자열과 정수를 연결하려고합니다.

print (numlist.pop (2) + "has been removed") 를 다음 중 하나로 변경합니다.

int 에서 str 로의 명시 적 변환 :

print(str(numlist.pop(2)) + " has been removed")

+ 대신 , 사용 :

print(numlist.pop(2), "has been removed")

문자열 형식 :

print("{} has been removed".format(numlist.pop(2)))

 

참조 페이지 https://stackoverflow.com/questions/20441035

 

 

반응형

댓글