Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 13th, 2013, 05:55 PM
 DiggerZane
Registered User

Join Date: Apr 2013
Posts: 19
Time spent in forums: 4 h 36 m 48 sec
Reputation Power: 0
Random question help

Hello,
I'm having this problem with random questions. This is what I am trying to do. In normal python 3.3 I have a math game. The code below is what the questions look like in normal 3.3 python.
Code:
``` import random
Qa=random.randint(1, 10)
Qab=random.randint(1, 10)
print ('Question 1:   ' + str (Qa) +' + ' + str (Qab) + ' = ?') # separate the question number please!
Qbaa=input()      # Qba is a string
Qbaaa = Qa + Qab  # Qbaa is an integer
if Qbaa == str(Qbaaa):   # compare as strings.   str(Qbaa)
print ('Correct! + 25 to score!')
Score = Score + 25
print (Score)
else:
print ('Incorrect,  -25 to score!')
Score = Score - 25
print (Score)```

That is what I'm trying to put in my Tkinter code.
Tkinter Code:
Code:
```        def callback5():
label = Label(root, textvariable=var, relief=RAISED)
var.set ("You are about to start the game!")
label.pack()
Qa=random.randint(1, 10)
Qab=random.randint(1, 10)
Qabb=Qa + Qab
label = Label(root, textvariable=var, relief=RAISED)
var.set ("Question 1: " + Qa + " + " + Qab + " = ? ")
label.pack()
label = Label(root, textvariable=var, relief=RAISED)
var.set ("Correct!")
label.pack()

b = Button(root, text="Play Game", command=callback5)
b.pack()
b = Button(root, text="Ranks", command=callback4)
b.pack()
b = Button(root, text="About the Game", command=callback3)
b.pack()```

Thank you.

#2
April 14th, 2013, 11:06 AM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 575
Time spent in forums: 5 Days 4 h 12 m 10 sec
Reputation Power: 63
You keep creating the same label object.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

#3
April 15th, 2013, 11:36 AM
 DiggerZane
Registered User

Join Date: Apr 2013
Posts: 19
Time spent in forums: 4 h 36 m 48 sec
Reputation Power: 0
Quote:
 Originally Posted by Dietrich You keep creating the same label object.

Well, sorry I guess I did not make my problem clear. I was wondering if
Code:
`Qa = random.randint(1, 10)`
Works in Tkinter?

#4
April 15th, 2013, 11:54 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,168
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 55 m 17 sec
Reputation Power: 455
Don't create the Tkinter.Label in the callback.
Do create them in the main code, just once.
In the callback change the value of the text variable.
__________________
[code]Code tags[/code] are essential for python code!

#5
April 16th, 2013, 10:57 AM
 DiggerZane
Registered User

Join Date: Apr 2013
Posts: 19
Time spent in forums: 4 h 36 m 48 sec
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg Don't create the Tkinter.Label in the callback. Do create them in the main code, just once. In the callback change the value of the text variable.

okay I will try that if I know what you mean.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Random question help