Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8649533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:35:06+00:00 2026-06-12T13:35:06+00:00

We have to make a test for elementary school kids to test there skills

  • 0

We have to make a test for elementary school kids to test there skills in math. I need the label on the left to have a random equation, and the right have an entry form, and the far right to have a button that checks if the answer they gave is correct. If they got it right a new equation is given.

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

class Ed(Frame):
    'Simple arithmetic education app'
    def __init__(self,parent=None):
        'constructor'
        Frame.__init__(self, parent)
        self.pack()
        Ed.new_problem(self)
        Ed.make_widgets(self)
        self.tries = 0

    def make_widgets(self):
        'defines Ed widgets'
        if self.plusminus == 1: 
            Label(self, text=(self.a ,"+" ,self.b)).pack(side=LEFT)
        else:
            Label(self, text=(self.a, "-" ,self.b)).pack(side=LEFT)       
        self.ent = Entry(self)
        self.ent.pack(side=LEFT)        
        Button(self, text='Enter', command=self.evaluate).pack(side=RIGHT)

    def new_problem(self):
        'creates new arithmetic problem'
        self.a = randrange(1,10,1)
        self.b = randrange(1,10,1)
        self.c = randrange(1,10,1)
        if self.c < 5:
            self.total = self.a + self.b
            self.plusminus = 1     #plusminus = plus
        else:
            self.total = self.a - self.b
            self.plusminus = 0     #plusminus = minus

    def evaluate(self):
        'handles button "Enter" clicks by comparing answer in entry to correct result'
        if self.total == eval(self.ent.get()):
            showinfo(title='YAY', message='You are CORRECT!')
            self.ent.delete(0,END)
        else:
            self.ent.delete(0,END)
            self.ent.insert(END, 'Wrong. Try again.')
            self.tries+=1


Ed().mainloop()

The problem is the equation does not update when the answer is correct. So when I enter 12 when the question is 8 + 4. It cays correct but keeps 8 + 4 tho I want it to change to a new equation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:35:08+00:00Added an answer on June 12, 2026 at 1:35 pm

    You’re only calling new_problem when your Ed object is instantiated. It’s never called again afterwards, and doing so won’t actually change the existing set up, as you only create the labels for your widget once, also at instantiation.

    What you need to do is pass in a tkinter.StringVar to your Label widget, and then set the arithmetic problem on that variable. You then run new_problem after each successful answer.

    I’ve updated your code slightly to use super and run methods directly on the instance rather than via the class, but this should work:

    from tkinter import Label, Frame, Entry, Button, LEFT, RIGHT, END, TOP, END, StringVar
    from tkinter.messagebox import showinfo
    from random import randrange
    
    class Ed(Frame):
        """Simple arithmetic education app"""
        def __init__(self,parent=None):
            """constructor"""
            super().__init__(parent)
            self.tries = 0
            self.problem = StringVar()
            self.make_widgets()
            self.new_problem()
            self.pack()
    
        def make_widgets(self):
            """defines Ed widgets"""
            self.label = Label(self, textvariable=self.problem)
            self.label.pack(side=LEFT)
            self.ent = Entry(self)
            self.ent.pack(side=LEFT)
            Button(self, text='Enter', command=self.evaluate).pack(side=RIGHT)
    
        def new_problem(self):
            """creates new arithmetic problem"""
            self.tries = 0
            self.a = randrange(1,10,1)
            self.b = randrange(1,10,1)
            if randrange(1,10,1) < 5:
                self.total = self.a + self.b
                self.plusminus = '+'
            else:
                self.total = self.a - self.b
                self.plusminus = '-'
            self.problem.set( (self.a , self.plusminus, self.b) )
    
        def evaluate(self):
            """handles button "Enter" clicks by comparing answer in entry to correct result"""
            if self.total == int(self.ent.get()):
                showinfo(title='YAY', message='You are CORRECT!')
                self.ent.delete(0,END)
                self.new_problem()
            else:
                self.ent.delete(0,END)
                self.ent.insert(END, 'Wrong. Try again.')
                self.tries += 1
    
    Ed().mainloop()
    

    I’ve also changed your eval to an int…you really don’t want to be doing that, because it enables users to inject python code into your application.

    For example, I can type the following as my “answer”:

    showinfo(title="Blah", message="Look ma - No hands!")
    

    That would just bring up a simple message box, but the door is wide open for more involved mischief.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a little help here.So basically I have to make a test of
I need to make performance test of Mule ESB. I have an access to
For example I have make test # do other stuff 'make test' runs some
I have been trying to test my application to make sure that all the
I have a simple Makefile: default: @make build @make run build: @javac Test.java >
I have a string $string = test<String>; but when i make echo $string; it
I have a series of CodedUI test methods that make up a single test
gcc 4.4.2 I have installed apache runtime portable. apr-1.3.9 ./configure make make test make
i have make a small example to test my Android environment, when i launch
When I run make test using the normal test harness that CPAN modules have,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.