python - Why does my variable not increment after one time? -
when user submits answer , wrong, def getresult() should make "mistake" variable goes 1 when user makes first error , mistake changes 0 1. after that, when user enter wrong answer, variable still ends "1" again. there way increment when user wrong?
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: 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 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 title screen",width=30,command=backtotitle) canvas.create_window(400, 200, window=backbutton) def backtotitle(): canvas.delete(all) titlescreen() def drawpole(): canvas.delete(all) = 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 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()
you first have declare mistake
variable right after imports:
mistake=0
if don't this, mistake
variables in functions local.
Comments
Post a Comment