Aidan H
Aidan H

Reputation: 21

Issues pulling value from file and using it in calculation

My friend's having issues with this code!

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)

We get this error:

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'

Tried changing the variables to int( and all kinds of things but to no luck, any comments or help would be appreciated!

Upvotes: 0

Views: 106

Answers (1)

R Hyde
R Hyde

Reputation: 10409

Try this:

Strengthmod = int(Strength) - int(Strength2)

The problem with the line that you had was that you were trying to subtract one string from another which (as the error message said) isn't supported.

Upvotes: 2

Related Questions