친구가이 코드에 문제가 있습니다!파일에서 값을 가져 와서 계산에서 사용하는 문제
file = open('attributes.txt', 'r')
Name = file.readline()
Name = Name[1:]
Name = Name[:-2]
print(Name)
Strength = file.readline()
Strength = Strength[-3:-1]
print(Strength)
Skill = file.readline()
Skill = Skill[-3:-1]
print(Skill)
Name2 = file.readline()
Name2 = Name2[1:]
Name2 = Name2[:-2]
print(Name2)
Strength2 = file.readline()
Strength2 = Strength2[-3:-1]
print(Strength2)
Skill2 = file.readline()
Skill2 = Skill2[-3:-1]
print(Skill2)
Strengthmod = int((Strength) - (Strength2))
if Strengthmod < 0:
Strengthmod = 0
print("Character dies")
print(Strengthmod)
우리는이 오류를 얻을 :!
Traceback (most recent call last):
File "S:/Computing/Course Work/A453 - Python/Task Three", line 23, in <module>
Strengthmod = int((Strength) - (Strength2))
TypeError: unsupported operand type(s) for -: 'str' and 'str'
이 int로 변수를 변경 시도 (사물의하지만 행운에 모든 종류의, 의견이나 도움을 주시면 감사하겠습니다을
'int (Strength) - int (Strength2)'를 원한다. 계산을하기 전에 데이터를 숫자로 변환해야합니다. – Matthias
안녕하세요. 아래 답변 중 하나가 문제를 해결하면 해당 답변 옆에있는 체크 표시를 클릭하여 동의해야합니다. 그것은 두 가지 일을합니다. 모든 사람에게 문제가 해결되었음을 알리고 도움을 제공하는 사람에게 도움을줍니다. 자세한 설명은 [here] (http://meta.stackoverflow.com/help/someone-answers)를 참조하십시오. 감사! – nodakai
팁 : 변수의 이름을 대문자로 사용하지 마십시오. – Bonifacio2