Development Tip

TypeError : 'int'개체를 암시 적으로 str로 변환 할 수 없습니다.

yourdevel 2020. 11. 27. 21:31
반응형

TypeError : 'int'개체를 암시 적으로 str로 변환 할 수 없습니다.


나는 텍스트 게임을 작성하려고하는데 캐릭터를 만든 후에 기본적으로 스킬 포인트를 사용할 수 있도록 정의하는 기능에 오류가 발생했습니다. 처음에 오류는 코드의이 부분에서 정수에서 문자열을 빼려고 시도했음을 나타 balance - strength냅니다.. 분명히 그것은 잘못되었으므로 수정했습니다 strength = int(strength)...하지만 이제는 이전에 본 적이없는이 오류가 발생합니다 (새 프로그래머). 정확히 무엇을 말하려고하는지, 어떻게 수정하는지에 대해 난처합니다.

작동하지 않는 함수 부분에 대한 코드는 다음과 같습니다.

def attributeSelection():
    balance = 25
    print("Your SP balance is currently 25.")
    strength = input("How much SP do you want to put into strength?")
    strength = int(strength)
    balanceAfterStrength = balance - strength
    if balanceAfterStrength == 0:
        print("Your SP balance is now 0.")
        attributeConfirmation()
    elif strength < 0:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif strength > balance:
        print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
        attributeSelection()
    elif balanceAfterStrength > 0 and balanceAfterStrength < 26:
        print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.")
    else:
        print("That is an invalid input. Restarting attribute selection.")
        attributeSelection()

다음은 쉘의 코드에서이 부분에 도달했을 때 발생하는 오류입니다.

    Your SP balance is currently 25.
How much SP do you want to put into strength?5
Traceback (most recent call last):
  File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 205, in <module>
    gender()
  File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 22, in gender
    customizationMan()
  File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 54, in customizationMan
    characterConfirmation()
  File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 93, in characterConfirmation
    characterConfirmation()
  File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 85, in characterConfirmation
    attributeSelection()
  File "C:\Python32\APOCALYPSE GAME LIBRARY\apocalypseGame.py", line 143, in attributeSelection
    print("Ok. You're balance is now at " + balanceAfterStrength + " skill points.")
TypeError: Can't convert 'int' object to str implicitly

누구든지 이것을 해결하는 방법을 알고 있습니까? 미리 감사드립니다.


a stringint. 당신은 당신을 변환 할 필요가 intA를 string사용 str기능을 사용하거나 formatting귀하의 출력 형식을.

변경 :-

print("Ok. Your balance is now at " + balanceAfterStrength + " skill points.")

받는 사람 :-

print("Ok. Your balance is now at {} skill points.".format(balanceAfterStrength))

또는 :-

print("Ok. Your balance is now at " + str(balanceAfterStrength) + " skill points.")

또는 주석에 따라 다음을 사용 ,하여 print연결하는 대신 함수에 다른 문자열을 전달 하는 사용하십시오 +.

print("Ok. Your balance is now at ", balanceAfterStrength, " skill points.")

def attributeSelection():
balance = 25
print("Your SP balance is currently 25.")
strength = input("How much SP do you want to put into strength?")
balanceAfterStrength = balance - int(strength)
if balanceAfterStrength == 0:
    print("Your SP balance is now 0.")
    attributeConfirmation()
elif strength < 0:
    print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
    attributeSelection()
elif strength > balance:
    print("That is an invalid input. Restarting attribute selection. Keep an eye on your balance this time!")
    attributeSelection()
elif balanceAfterStrength > 0 and balanceAfterStrength < 26:
    print("Ok. You're balance is now at " + str(balanceAfterStrength) + " skill points.")
else:
    print("That is an invalid input. Restarting attribute selection.")
    attributeSelection()

참고 URL : https://stackoverflow.com/questions/13654168/typeerror-cant-convert-int-object-to-str-implicitly

반응형