26099
26099

Reputation: 91

Subtracting and adding strings in python 3.0

I want to subtract two strings like so:
health = "100"
nuke = "50"
health = health - nuke
print(health)

It is meant to assign the new value '50' as the health but when i do this i get: TypeError: unsupported operand type(s) for -: 'str' and 'str'

Upvotes: 1

Views: 55

Answers (2)

26099
26099

Reputation: 91

health = 100
nuke = 50
health = health - nuke
print(health)

This keeps the code nice and tidy instead of having to change things into intergers

Upvotes: 0

awesoon
awesoon

Reputation: 33651

Python is not js, you cannot subtract string from string. Convert them to number:

print(int(health) - int(nuke))

Upvotes: 1

Related Questions