You cant subtract two string, cast them first to integers.
Subtract = int(enter2) - int(enter)
Answer from jabaldonedo on Stack OverflowYou cant subtract two string, cast them first to integers.
Subtract = int(enter2) - int(enter)
your problem is that by using the eg.enterbox function, the numbers the user inputs will be seen as strings. To fix this, we send the 'string' the enterbox returns, to the int() function. This will translate the string of numbers to an actual integer that can be used to subtract other integers.
eg.
enter = "3"
enter = int(enter)
enter2 = "4" #This is an example of what the variable looks like coming from the enterbox
enter2 = int(enter2) #enter2 now looks like 4 instead of "4"
output = enter2 - enter #this will return 1 to the variable output
Hope this helps ;)
oh, and you don't need the quotation marks in this line:
if enter < '0' or enter > '999':
as you are saying that '0' is a string, and not to be treated like a number ;)
http://bpaste.net/show/3X286y8UrNDihnngNapG/
It says that line 18 is "TypeError: unsupported operand type(s) for -: 'unicode' and 'str' "
What can I do to fix this?
Hey guys, so i'm trying to write a script where you input your age which then is subtracted from a set number (In this case it's 40)
So basically what i'd like to do is write like this
# Question
print('How old are you?:')
age = input()
x = 40
# Result
print('Hello' + ' ' + name + ' ' + 'you retire in' + ' ' + x - age + ' ' + 'years.')
This is not possible since "x" is a set number and "age" is a string which wont subtract.
I've been trying this out with float but haven't got any further than that.
Any help would be appreciated!