Ok. Newbie poster/programmer so bear with me. I created a beginner math program that should be usable by young children. I have it working for the most part but now there are three things I would like to do to make the program more professional. First, I would now like to create random questions and answers, rather than typing out the questions and answer individually as I have done. Second, for whatever reason the correct answer is always the top right left (You will see what i mean when running the program). Lastly, if a wrong answer is clicked on, the program just moves on to the next question. I would like it to repeat the question until they get it right. I feel like the answers are simple, right under my nose and that I just don't see them. I know it is a lot to ask for but if anyone could help me out with any of these in even the smallest way that would be much appreciated. Thanks all!
P.S. I know the code is long so if anyone knows any way of shortening it that would be helpful as well

``````from graphics import *

win = GraphWin('Choose', 700,700)

def inside(rect, pt):

if (pt.getX() > rect.getP1().getX()) and (pt.getX() < rect.getP2().getX()) and (pt.getY() > rect.getP1().getY()) and (pt.getY() < rect.getP2().getY()):
return True
return False

def makeColoredRect(corner, width, height, color, win):

corner2 = corner.clone()
corner2.move(width, -height)
rect = Rectangle(corner, corner2)
rect.setFill(color)
rect.setWidth(2)
rect.setOutline("black")
rect.draw(win)
corner3 = corner.clone()
corner3.move(width/2, -height/2)
return rect

def Button(corner, width, height, color, win):

corner2 = corner.clone()
corner2.move(width, -height)
rect = Rectangle(corner, corner2)
rect.setFill(color)
rect.setWidth(2)
rect.setOutline("black")
rect.draw(win)
corner3 = corner.clone()
corner3.move(width/2, -height/2)
return rect

Aquestions = ('what is 1+1','what is 2+1','what is 8+8')
Acorrect= (2,3,16)
Awrong = ((1,4,7),(3,5,4),(3,34,43))

for q in range(len(Aquestions)):
Question= Text(Point(350,200), Aquestions[q])
Question.draw(win)
Correct = Button(Point(100,500), 200, -50, 'Red', win)
textCorrect = Text(Point(200, 525),Acorrect[q])
textCorrect.draw(win)
Wrong1 = Button(Point(100,600), 200, -50, 'Red', win)
textWrong1 = Text(Point(200, 625),Awrong[q][0])
textWrong1.draw(win)
Wrong2 = Button(Point(400,500), 200, -50, 'Red', win)
textWrong2 = Text(Point(500, 525),Awrong[q][1])
textWrong2.draw(win)
Wrong3 = Button(Point(400,600), 200, -50, 'Red', win)
textWrong3 = Text(Point(500, 625),Awrong[q][2])
textWrong3.draw(win)

click = win.getMouse()

if inside (Correct,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
Yay = Button(Point(200,100), 300, -200, 'Red', win)
textYay = Text(Point(350, 200),"Good Job")
textYay.draw(win)
win.getMouse()
textYay.undraw()

if inside (Wrong1,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong2,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong3,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

#Subtraction Section
def StartSubtract():

Squestions  = list(['what is 3-2','what is 5-3'])
Scorrect= (1,2)
Swrong = ((2,4,7),(2,5,4))

for q in range(len(Squestions)):
Question= Text(Point(350,200), Squestions[q])
Question.draw(win)
Correct = Button(Point(100,500), 200, -50, 'Red', win)
textCorrect = Text(Point(200, 525),Scorrect[q])
textCorrect.draw(win)
Wrong1 = Button(Point(100,600), 200, -50, 'Red', win)
textWrong1 = Text(Point(200, 625),Swrong[q][0])
textWrong1.draw(win)
Wrong2 = Button(Point(400,500), 200, -50, 'Red', win)
textWrong2 = Text(Point(500, 525),Swrong[q][1])
textWrong2.draw(win)
Wrong3 = Button(Point(400,600), 200, -50, 'Red', win)
textWrong3 = Text(Point(500, 625),Swrong[q][2])
textWrong3.draw(win)

click = win.getMouse()

if inside (Correct,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
Yay = Button(Point(200,100), 300, -200, 'Red', win)
textYay = Text(Point(350, 200),"Good Job")
textYay.draw(win)
win.getMouse()
textYay.undraw()

if inside (Wrong1,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong2,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong3,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

#Division Section
def StartDivide():

Dquestions  = list(['what is 4/2','what is 9/3'])
Dcorrect= (2,3)
Dwrong = ((1,4,5),(2,6,8))

for q in range(len(Dquestions)):
Question= Text(Point(350,200), Dquestions[q])
Question.draw(win)
Correct = Button(Point(100,500), 200, -50, 'Red', win)
textCorrect = Text(Point(200, 525),Dcorrect[q])
textCorrect.draw(win)
Wrong1 = Button(Point(100,600), 200, -50, 'Red', win)
textWrong1 = Text(Point(200, 625),Dwrong[q][0])
textWrong1.draw(win)
Wrong2 = Button(Point(400,500), 200, -50, 'Red', win)
textWrong2 = Text(Point(500, 525),Dwrong[q][1])
textWrong2.draw(win)
Wrong3 = Button(Point(400,600), 200, -50, 'Red', win)
textWrong3 = Text(Point(500, 625),Dwrong[q][2])
textWrong3.draw(win)

click = win.getMouse()

if inside (Correct,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
Yay = Button(Point(200,100), 300, -200, 'Red', win)
textYay = Text(Point(350, 200),"Good Job")
textYay.draw(win)
win.getMouse()
textYay.undraw()

if inside (Wrong1,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong2,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong3,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

#Multiplication Section
def StartMultiply():

Mquestions  = list(['what is 2 x 2','what is 3 x 3'])
Mcorrect= (4,9)
Mwrong = ((3,6,9),(2,4,6))

for q in range(len(Mquestions)):
Question= Text(Point(350,200), Mquestions[q])
Question.draw(win)
Correct = Button(Point(100,500), 200, -50, 'Red', win)
textCorrect = Text(Point(200, 525),Mcorrect[q])
textCorrect.draw(win)
Wrong1 = Button(Point(100,600), 200, -50, 'Red', win)
textWrong1 = Text(Point(200, 625),Mwrong[q][0])
textWrong1.draw(win)
Wrong2 = Button(Point(400,500), 200, -50, 'Red', win)
textWrong2 = Text(Point(500, 525),Mwrong[q][1])
textWrong2.draw(win)
Wrong3 = Button(Point(400,600), 200, -50, 'Red', win)
textWrong3 = Text(Point(500, 625),Mwrong[q][2])
textWrong3.draw(win)

click = win.getMouse()

if inside (Correct,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
Yay = Button(Point(200,100), 300, -200, 'Red', win)
textYay = Text(Point(350, 200),"Good Job")
textYay.draw(win)
win.getMouse()
textYay.undraw()

if inside (Wrong1,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong2,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

if inside (Wrong3,click):
Correct.undraw()
textCorrect.undraw()
Wrong1.undraw()
textWrong1.undraw()
Wrong2.undraw()
textWrong2.undraw()
Wrong3.undraw()
textWrong3.undraw()
No = Button(Point(200,100), 300, -200, 'Red', win)
textNo = Text(Point(350, 200),"Wrong")
textNo.draw(win)
win.getMouse()
textNo.undraw()

def AnsButton(corner, width, height, color, win, label):

corner2 = corner.clone()
corner2.move(width, -height)
rect = Rectangle(corner, corner2)
rect.setFill(color)
rect.setWidth(2)
rect.setOutline("black")
rect.draw(win)
corner3 = corner.clone()
corner3.move(width/2, -height/2)
l1 = Text(corner3,label)
l1.draw(win)
return rect

def main():

Add = makeColoredRect(Point(87.5, 43.75), 525, -87.5, 'Red', win)
Subtract = makeColoredRect(Point(87.5, 175), 525, -87.5, 'Blue', win)
textSubtract = Text(Point(350, 218.75),'Subtract')
textSubtract.draw(win)
Divide = makeColoredRect(Point(87.5, 306.25), 525, -87.5, 'Pink', win)
textDivide = Text(Point(350, 350),'Divide')
textDivide.draw(win)
Multiply = makeColoredRect(Point(87.5, 437.5), 525, -87.5, 'Purple', win)
textMultiply = Text(Point(350, 481.25),'Multiply')
textMultiply.draw(win)
click= win.getMouse()

Subtract.undraw()
textSubtract.undraw()
Divide.undraw()
textDivide.undraw()
Multiply.undraw()
textMultiply.undraw()

elif inside(Subtract, click):

StartSubtract()

elif inside(Divide, click):
StartDivide()

elif inside(Multiply, click):
StartMultiply()

main()``````
4
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by jrcagle

Just a note:
The library (graphics.py) is a wrapper for the Tkinter GUI toolkit and is designed to make it very easy for novice programmers to
experiment with computer graphics in an object oriented fashion. It is
written by John Zelle for use with the book "Python Programming: An
Introduction to Computer Science" (Franklin, Beedle & Associates).

It is not in very common use and help might be more difficult to find.

Don't think I can help with your GUI issues but, this little quickie demonstrates logic that could be used to generate random math problems and loop until the correct answer is give.

``````import random

def randomProblem():
"""Makes basic arithmetic problems"""
random.seed()
operator = {     # Dictionary of basic operators
1 : '+',
2 : '-',
3 : '*',
4 : '/'
}
op = random.randint(1, 4) # Generate a number being 1, 2, 3 or 4
a = random.randint(1, 10)
b = random.randint(1, 10)

# Solve the problem based on the randomly chosen operator
if op is 1:
ans = a + b
elif op is 2:
ans = a - b
elif op is 3:
ans = a * b
elif op is 4:
ans = a / b
else:
return None

# Change 'op' to the string to be printed
op = operator[op]

return a, op, b, ans

def main():
status = False
while status is not 'q':
part = randomProblem()

# Stick everything together
current_prob = str(part[0]) + part[1] + str(part[2]) + " ="
print current_prob
correct = False
while not correct:
print "Thats not right, Try again"
else:
correct = True
status = raw_input("'q' to quit, Enter for next")
main()``````

Well, the code that you are using for addition, subtraction, multiplication, and division problems is all very redundant.

I would work on getting a more general problem solved:

* Pick an operator from +,-,*,/.
* Create a problem
* (1) Display it onscreen
* Get user input.
* If right, new problem; if not, back to (1)

Second, since I don't have the graphics module installed here, what kind of output are you getting?

Jeff