Ошибка ValueError: invalid literal for int() with base 10
Учу питон и решил написать небольшую игру для закрепления знаний. Столкнулся с ошибкой перед запуском программы
ValueError: invalid literal for int() with base 10: ''
from tkinter import *
import time
import random as r
root = Tk()
root.title('Tkinter')
root.geometry('200x200')
num1 = str(r.randint(1, 24))
num2 = str(r.randint(0, 24))
a = str(num1) + ' * ' + str(num2)
b = 'Награда ' + str(r.randint(1,100)) + ' кредита(-ов)!'
c = int(num1) * int(num2)
def question():
global answer
global answer_string
global answer_int
label = Label(root, text='Решите пример:').grid(row=0, column=0)
math = Label(root, text=a).grid(row=0, column=1)
gift = Label(root, text=b).grid(row=1)
btn = Button(root, text='Проверить')
btn.bind('<Button-1>', answer_check)
btn.grid(row=3)
answer = Entry(root)
answer.grid(row=2)
answer_string = answer.get()
answer_int = int(answer_string)
def answer_check(event):
if answer_int == c:
Label(root, text='True').grid(row=4)
else:
Label(root, text='False').grid(row=4)
question()
root.resizable(False, False)
root.mainloop()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
from tkinter import *
import time
import random as r
root = Tk()
root.title('Tkinter')
root.geometry('200x200')
num1 = str(r.randint(1, 24))
num2 = str(r.randint(0, 24))
a = str(num1) + ' * ' + str(num2)
b = 'Награда ' + str(r.randint(1,100)) + ' кредита(-ов)!'
c = int(num1) * int(num2)
def question():
global answer
global answer_string
global answer_int
label = Label(root, text='Решите пример:').grid(row=0, column=0)
math = Label(root, text=a).grid(row=0, column=1)
gift = Label(root, text=b).grid(row=1)
btn = Button(root, text='Проверить')
btn.bind('<Button-1>', answer_check)
btn.grid(row=3)
answer = Entry(root)
answer.grid(row=2)
# answer_string = answer.get() # ---
# answer_int = int(answer_string) # ---
def answer_check(event):
answer_string = answer.get() # +++
answer_int = int(answer_string) if answer_string else -1 # +++
if answer_int == c:
Label(root, text='True').grid(row=4)
else:
Label(root, text='False').grid(row=4)
question()
root.resizable(False, False)
root.mainloop()
