我即将完成我用python制作的数学游戏,但我收到错误代码“未定义num 1和num2,这是我的python课程的最后一部分,只有这阻碍了我完成它所以我非常感谢我能得到的所有帮助。数字是在显示函数中确定的 int,并且在显示函数中添加一次尝试并基于该消息是否正确或错误给出消息。问题是除了主要功能是乘法。我已经删除了小部件,因为我认为它们对于解决这个问题并不重要。
from Tkinter import *
import random
number = num1 * num2
num1= random.randint(1, 15)
global num1
num2 = random.randint(1, 15)
global num2
forsok = 0
class Application(Frame):
def quit(self):
global root
root.destroy()
def reset(self):
self.name_ent.delete(0, END)
self.gissa_ent.delete(0, END)
self.display1_txt.delete(1.0,END)
self.display2_txt.delete(1.0,END)
self.display3_txt.delete(1.0,END)
self.display4_txt.delete(1.0,END)
def reveal(self):
global forsok
name = self.name_ent.get()
gissa = self.gissa_ent.get()
if int(gissa) != int(number):
result_msg = "Tyvärr det är fel!!"
forsok += 1
if int(gissa) == int(number):
result_msg = "Du gissade rätt!!Grattis du vann!"
forsok += 1
welcome_msg = "Välkommen " + name
gissa_msg = " Du svarade på: " + gissa
forsok_msg = "Du har svarat " + str(forsok) + "gånger."
if forsok > 3:
welcome_msg = "Game OVER!!."
gissa_msg = "Du har gissat för många gånger mohahaha."
result_msg = " "
forsok_msg = " "
# Display visar de olika meddelanadena
self.display1_txt.delete(0.0, END)
self.display1_txt.insert(0.0, welcome_msg)
self.display2_txt.delete(0.0, END)
self.display2_txt.insert(0.0, gissa_msg)
self.display3_txt.delete(0.0, END)
self.display3_txt.insert(0.0, result_msg)
self.display4_txt.delete(0.0, END)
self.display4_txt.insert(0.0, forsok_msg)
# Main hanteraren rootar och bestämmer storleken på fönstret
root = Tk()
root.title("Gissa Multiplikationen")
root.geometry("700x450")
app = Application(root)
root.mainloop()
root = Tk()
quit_bttn(root)
Application(root)
root.mainloop()
错误消息如下
number = num1 * num2
NameError: global name 'num1' is not defined