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

New Free Tools on Dev Shed!

#1
April 25th, 2013, 06:27 PM
 DiggerZane
Registered User

Join Date: Apr 2013
Posts: 19
Time spent in forums: 4 h 36 m 48 sec
Reputation Power: 0
My 2nd Question not working

Hello,
Well so I got my first question to work fine, but the second question is just the same as the first one every time. Not all of the code below is needed.
Code:
```from Tkinter import *
root = Tk()
rank=0
points=0
root.geometry("900x1000+200+200")
root.title("Math Game")
var = StringVar()
var2= StringVar()
var3= StringVar()
var4= StringVar()
var5= StringVar()
var6= StringVar()
var7= StringVar()
var8= StringVar()
var9= StringVar()
var10= StringVar()
var11= StringVar()
var12= StringVar()
var13= StringVar()
var14= StringVar()
var15= StringVar()
label1=Label(root, textvariable=var, relief=RAISED)
var.set ("Welcome To the Math Game set 1")
label1.pack()
label2=Label(root, textvariable=var2, relief=RAISED)
label2.pack()
def callback1():
label3=Label(root, textvariable=var3, relief=RAISED)
label3.pack()
label4=Label(root, textvariable=var4, relief=RAISED)
var4.set("The Math Game is a good way to learn how to do math facts as well as doing the problems your self. There is also games with it!")
label4.pack()

def callback2():
label5=Label(root, textvariable=var5, relief=RAISED)
var5.set("Rank: " + str (rank))
label5.pack()

def callback3():
label6=Label(root, textvariable=var6, relief=RAISED)
var6.set("(10 rank = 1 point) Points are used to buy more levels and earn many differnt things. you have " + str (points) + " Points!")
label6.pack()

def callback4():
label7=Label(root, textvariable=var7, relief=RAISED)
var7.set("You are about to start the game!")
label7.pack()
import random
Questiona = random.randint(1, 10)
Questionab = random.randint(1, 10)
label8 = Label(root, textvariable=var12, relief=RAISED)
var8.set("Question 1: " + str (Questiona) + " + " + str (Questionab) + " = ?")
label8.pack()
def callback5():
label9 = Label(root, textvariable=var9, relief=RAISED)
var9.set("Correct! + 25 to your score!")
label9.pack()
label10 = Label(root, textvariable=var10, relief=RAISED)
var10.set("Score: " + str (points))
label10.pack()
b4.destroy()
else:
label11 = Label(root, textvariable=var11, relief=RAISED)
label11.pack()
b4.destroy()
b4 = Button(root, text="submit", width=15, command=callback5)
b4.pack()

import random
Questionb = random.randint(1, 10)
Questionbb = random.randint(1, 10)
label12 = Label(root, textvariable=var12, relief=RAISED)
var12.set("Question 1: " + str (Questionb) + " + " + str (Questionbb) + " = ?")
label12.pack()
def callback6():
label13 = Label(root, textvariable=var13, relief=RAISED)
var13.set("Correct! + 25 to your score!")
label13.pack()
label14 = Label(root, textvariable=var14
, relief=RAISED)
var14.set("Score: " + str (points))
label14.pack()
b6.destroy()
else:
label15 = Label(root, textvariable=var15, relief=RAISED)
label15.pack()
b6.destroy()
b6 = Button(root, text="submit", width=15, command=callback6)
b6.pack()

b3=Button(root, text="Play Game", command=callback4)
b3.pack()
b2=Button(root, text="Points", command=callback3)
b2.pack()
b1=Button(root, text="Rank", command=callback2)
b1.pack()
b.pack()```

#2
April 26th, 2013, 08:00 AM
 partoj
Contributing User

Join Date: Feb 2013
Posts: 138
Time spent in forums: 1 Day 12 h 26 m 47 sec
Reputation Power: 1
I'm sorry, but I cannot decipher your code.

My suggestions:
* Use proper, descriptive variable names, "var12" and "b6" are too cryptic and short
* Separate the game logic and gui parts
* Use either CamelCase or underscore_case syntax for your variables. It took me about 10 seconds to decipher what "Questionbansweruser" actually was referring to
* Put imports on top of your code, no need to import random twice
__________________
http://www.catb.org/esr/faqs/smart-questions.html#code

#3
April 26th, 2013, 09:21 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,132
Time spent in forums: 1 Month 3 Weeks 2 Days 5 h 52 m 54 sec
Reputation Power: 455

Code:
```import tkinter
root = tkinter.Tk()
root.withdraw()
list_of_16_strvars = [tkinter.StringVar() for i in range(16)]```
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > My 2nd Question not working