简体   繁体   中英

I need to make my evaluate function keep track of how many tries it takes the user to get the correct answer(Python)

I know it must be done recursively(I think) but not quite sure how. in def evaluate it must keep track of how many times evaluate is called before a correct answer is given. Thats where I am stuck.

from tkinter import Label, Frame, Entry, Button, LEFT, RIGHT, END
from tkinter.messagebox import showinfo
from random import randrange

# Questions 1 and 2
class Ed(Frame):
    'Simple arithmetic education app'
     def __init__(self,parent=None):
         'constructor'
         Frame.__init__(self, parent)
         self.pack()
         Ed.make_widgets(self)
         Ed.new_problem(self)

     def make_widgets(self):
         'defines Ed widgets'
         self.entry1 = Entry(self,width = 15)
         self.entry1.pack(side = LEFT)
         self.entry2 = Entry(self,width = 15)
         self.entry2.pack(side = LEFT)
         self.button = Button(self, text = "Enter", command = self.evaluate)
         self.button.pack(side = RIGHT)

     def new_problem(self):
         'generates new numbers and decides whether + or -'
         self.entry1.delete(0,END)
         self.entry2.delete(0,END)
         num1 = randrange(0,10)
         num2 = randrange(0,10)
         signNum = randrange(1,3)
         if num1 < num2 and signNum == 2:
             self.new_problem()
         else:
             num1 = str(num1)
             num2 = str(num2)
             if signNum  == 1:
                 sign = "+"
             elif signNum == 2:
                 sign = "-"
             problem = (num1)+sign+(num2)
             self.entry1.insert(0,problem)
     def evaluate(self):
         'handles button "Enter" clicks by comparing answer in entry to correct  result'
         if eval(self.entry1.get()) == int(self.entry2.get()):
             showinfo(title = "Yes!",message = "You got it!")
             self.new_problem()
         else:
             self.entry2.delete(0,END)
     Ed.mainloop()

Add a instance variable in init:

def __init__(self,parent=None):
        'constructor'
        Frame.__init__(self, parent)
        self.pack()
        Ed.make_widgets(self)
        Ed.new_problem(self)
        self.triesCount = 0

And update it in evaluate:

def evaluate(self):
        'handles button "Enter" clicks by comparing answer in entry to correct  result'
        if eval(self.entry1.get()) == int(self.entry2.get()):
            showinfo(title = "Yes!",message = "You got it!")
            self.new_problem()
        else:
            self.entry2.delete(0,END)
            self.triesCount += 1

Or you could create a separate Problem class to encapsulate all of the problem operations and variables and do something similar to handle try counts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM