1 def iNput():
2 try:
3 int(entry.get())
4
5 x = entry.get()
6 y = x + 3
7
8 answer.config(text=y)
9
10 except ValueError:
11 answer.config(text="doesn't work")
In line 3, you are fetching the user input from the entry and converting it to int only to throw it away (because you are not doing anything with the int). In line 5, you fetch it again and assign it to x
, but this time without converting it to a number. So, you are trying to add a a string and a number in line 6. This cannot work (try to add the letter a
and the number 1
in your head !!!). If the user is meant to enter a number and you want display that number + 3
in answer
, the following code should work:
def iNput():
try:
x = int(entry.get())
y = str(x + 3) # x + 3 produces a number, which you have to convert back to a string
answer.config(text=y)
except ValueError:
answer.config(text="doesnt work")
The same can be done much shorter:
def iNput():
try:
answer.config(text=str(int(entry.get()) + 3))
except ValueError:
answer.config(text="doesnt work")