当用户提交答案及其错误时,def getResult()应该使“错误”变量在用户发出第一个错误并且错误从0更改为1时上升1.之后,当用户输入一个错误的答案,变量仍然会再次变为“1”。当用户出错时,有没有办法让它总是递增?
from tkinter import *
import random
root = Tk()
root.wm_title("HangMan")
canvas = Canvas(root, height=400, width=800)
canvas.pack()
name = ''
def titlescreen():
title = canvas.create_text(400, 100, font=("Times New Roman", 50), tags="title")
canvas.itemconfig(title, text="HANGMAN: THE GAME")
startButton = Button(canvas, text="Start Game", width=10, command=startgame)
canvas.create_window(400, 200, window = startButton)
creditsButton = Button(canvas, text="Credits", width=10, command=displaycredits)
canvas.create_window(400, 250, window = creditsButton)
def startgame():
def getName(nameEntry):
global name
name = nameEntry.get()
print (name)
drawPole()
canvas.delete(ALL)
welcome = canvas.create_text(400,100,font=("Times New Roman",50),text="WELCOME!")
welcome2 = canvas.create_text(400,150,font=("Times New Roman", 30),text="PLEASE ENTER YOUR NAME")
nameEntry = Entry(canvas)
enterName = Button(canvas, text="Enter", width=10, command=lambda: getName(nameEntry))
canvas.create_window(350, 200, window = nameEntry)
canvas.create_window(490, 200, window = enterName)
nameEntry.delete(0,END)
nameEntry.insert(0, "Enter Player Name")
def displaycredits():
canvas.delete(ALL)
created = canvas.create_text(400,100,font=("Times New Roman",50),text="CREATED BY")
credit1 = canvas.create_text(400,150,text="Patrick Tsui & Ricardo Perez")
credit2 = canvas.create_text(400,170,text="Montclair State University Students")
backButton = Button(canvas,text="Back to Title Screen",width=30,command=backtotitle)
canvas.create_window(400, 200, window=backButton)
def backtotitle():
canvas.delete(ALL)
titlescreen()
def drawPole():
canvas.delete(ALL)
a = canvas.create_rectangle(550,50,550,350,fill = "black")
b = canvas.create_rectangle(350,50,550,50,fill = "black")
c = canvas.create_rectangle(350,50,350,90,fill = "black")
gamePlay()
def gamePlay():
def getAnswer():
global answer
answer = answer1.get()
print (answer)
getResult()
def getResult():
global result2
global mistake
mistake = 0
result2 = result
if str(result2) == answer:
canvas.create_text(300, 375, text="Correct!",fill="green", tags="result")
else:
canvas.create_text(300, 375, text="Wrong!",fill="red", tags="result")
d = canvas.create_oval(325, 90, 375, 140, outline = "black")
mistake += 1
print (result2)
print (mistake)
Continue()
def Continue():
canvas.delete("confirm")
continueButton = Button(canvas,text="Continue",width=10,command=gamePlay)
canvas.create_window(450, 350, window = continueButton, tags="continue")
canvas.delete("question")
canvas.delete("continue")
canvas.delete("result")
x = random.randint(1,100)
y = random.randint(1,100)
questions = random.randint(0,3)
answer1 = Entry(canvas)
confirmButton = Button(canvas,text="Confirm",width=10,command=getAnswer)
canvas.create_window(300, 350, window = answer1)
canvas.create_window(450, 350, window = confirmButton, tags="confirm")
answer1.delete(0,END)
answer1.insert(0, "Enter Your Answer")
if questions == 0:
result = x + y
question1= canvas.create_text(350, 300, text=("Add: " + str(x) + " + " + str(y)), tags="question")
if questions == 1:
result = x - y
question1= canvas.create_text(350, 300, text=("Subtract: " + str(x) + " - " + str(y)), tags="question")
if questions == 2:
result = x * y
question1= canvas.create_text(350, 300, text=("Multiply: " + str(x) + " * " + str(y)), tags="question")
if questions == 3:
result = x // y
question1= canvas.create_text(350, 300, text=("Divide: " + str(x) + " / " + str(y)), tags="question")
titlescreen()
root.mainloop()
答案 0 :(得分:3)
首先必须在导入后立即声明mistake
变量:
mistake=0
如果您不这样做,您的函数中的所有mistake
变量都将是本地的。