[Tutor] Times Tables Program that constantly tells you that you are wrong!
Matthew Polack
matthew.polack at htlc.vic.edu.au
Thu Aug 16 03:18:33 EDT 2018
Hi All,
Thanks to your help I've nearly got my demo 'Times Tables' program fully
working.
The last remaining issue is the program does not calculate the percentage
right when you make mistakes...it just keeps giving a result of 100%.
I've looked and head scratched...and am not sure.
Can anyone figure out what is wrong with this code?
Thanks!
- Matt
from tkinter import *
import random
answer = 0
score = 0
wrong = 0
mistakes = 0
percentScore = 0
total = 0
def percentCheck():
global percentScore
global score
global mistakes
global total
percentScore = float(score/total) * 100
viewPercent()
def makeproblem():
# deletes anything in text box from location 00 to the end
global answer # access global answer
txt.delete(0.0, 'end')
sentence = "Here is your problem "
number1 = random.randint(2,12)
number2 = random.randint(2,12)
answer = number1 * number2
txt.insert(0.0, sentence)
txt.insert(2.2, number1)
txt.insert(3.3, " x ")
txt.insert(4.4, number2)
def checkanswer():
# deletes anything in text box from location 00 to the end
txt.delete(0.0, 'end')
global answer
global score
global wrong
global mistakes
global percentScore
global total
# checks for what is written in answerbox
response = int(answerbox.get())
wordScore = "Your score is now "
if response == answer:
score = int(score + 1)
total = int(total + 1)
result = "Great Job! "
root.update()
viewSC()
percentCheck()
viewPercent()
viewTotal()
else :
score= int(score + 1)
total = int(total + 1)
result = "Sorry...you were wrong"
mistakes = int(mistakes + 1)
viewWrong()
viewTotal()
percentCheck()
viewPercent()
txt.insert(0.0, result)
txt.insert(3.0, wordScore)
txt.insert(8.1, score)
# txt.insert(1,1, "Score is")
#txt.insert(3,3, score)
root = Tk()
root.geometry("640x700+0+0")
root.title("Times Tables")
timeslabel = Label(root, text="Times Tables Practice", fg="white",
bg="blue", font=("arial", 36, "bold"))
timeslabel.grid(columnspan=12, sticky='ew')
instruction = Label(root, text="Please click on the button to generate a
problem", fg="blue", bg="white", font=("arial", 16, "bold"))
instruction.grid(row=2, columnspan=20)
blankline = Label(root, text = "", bg = "white")
blankline.grid(row=12, sticky='ew')
# Makes an entry box with the variable of 'answerbox'
answerbox = Entry(root, bg="aqua", font=("arial", 24, "bold"))
answerbox.grid(row=15, columnspan=2, sticky=EW)
# Makes a button that generate the Times Tables problem
btn = Button(root, text="GENERATE PROBLEM", bg="blue", fg="white",
command=makeproblem)
btn.grid(row=11, columnspan=2, sticky=EW)
# Makes a button that checks the answer
btn = Button(root, text="CHECK ANSWER", bg="darkblue", fg="white",
command=checkanswer)
btn.grid(row=13, columnspan=2, sticky=EW)
#HOW CAN I CENTRE THE TEXT???
txt = Text(root, width=35, height=8, wrap=WORD, font=("arial", 24, "bold"))
txt.grid(row=12, columnspan=2, sticky=EW )
blankline3 = Label(root, text = "", bg = "white")
blankline3.grid(row=17, sticky='ew')
scorelab = Label(root, text="Score", bg="green", fg="white", font=("arial",
20, "bold"))
scorelab.grid(row=18, column=0, sticky=E)
totalLab = Label(root, text="Out of", bg="purple", fg="white",
font=("arial", 20, "bold"))
totalLab.grid(row=19, column=0, sticky=E)
wronglab = Label(root, text="Mistakes", bg="red", fg="white",
font=("arial", 20, "bold"))
wronglab.grid(row=20, column=0, sticky=E)
percentlab = Label(root, text="Percentage", bg="aqua", fg="white",
font=("arial", 20, "bold"))
percentlab.grid(row=21, column=0, sticky=E)
def viewSC():
global score
#scoreViewLab=Label(root, scoreView, textvariable=scoreView)
scoreViewLab = Label(root, text=score, fg="black", bg="grey",
font=("arial", 20, "bold"))
scoreViewLab.grid(row=18, column=1, sticky=W)
def viewTotal():
global total
#scoreViewLab=Label(root, scoreView, textvariable=scoreView)
totalViewLab = Label(root, text=total, fg="black", bg="grey",
font=("arial", 20, "bold"))
totalViewLab.grid(row=19, column=1, sticky=W)
def viewWrong():
global wrong
global mistakes
#scoreViewLab=Label(root, scoreView, textvariable=scoreView)
wrongViewLab = Label(root, text=mistakes, fg="black", bg="grey",
font=("arial", 20, "bold"))
wrongViewLab.grid(row=20, column=1, sticky=W)
def viewPercent():
global percentScore
global total
global score
#scoreViewLab=Label(root, scoreView, textvariable=scoreView)
percentViewLab = Label(root, text=percentScore, fg="black", bg="grey",
font=("arial", 20, "bold"))
percentViewLab.grid(row=21, column=1, sticky=W)
viewSC()
viewWrong()
viewPercent()
viewTotal()
root.mainloop()
Matthew Polack | Teacher
[image: Emailbanner3.png]
Trinity Drive | PO Box 822
Horsham Victoria 3402
p. 03 5382 2529 m. 0402456854
e. matthew.polack at htlc.vic.edu.au
w. www.htlc.vic.edu.au
On Wed, Aug 15, 2018 at 9:29 PM, Matthew Polack <
matthew.polack at htlc.vic.edu.au> wrote:
> Thanks Alan and Peter,
>
> Really appreciate your replies. Will try this out tomorrow and see if I
> can get it to work.
>
> I keep googling things myself...but with my 'beginner' status sometimes
> struggle to understand the solutions out there....but in tiny steps am
> making progress!
>
> Certainly know a lot more than when I started 4 weeks ago!
>
> Thank you so much for your support and time replying....it is very
> encouraging.
>
> - Matthew Polack
>
> Matthew Polack | Teacher
>
>
> [image: Emailbanner3.png]
>
> Trinity Drive | PO Box 822
>
> Horsham Victoria 3402
>
> p. 03 5382 2529 m. 0402456854
>
> e. matthew.polack at htlc.vic.edu.au
>
> w. www.htlc.vic.edu.au
>
> On Wed, Aug 15, 2018 at 8:10 PM, Alan Gauld via Tutor <tutor at python.org>
> wrote:
>
>> On 15/08/18 10:18, Peter Otten wrote:
>> > Matthew Polack wrote:
>> >
>> >> *Question 2:*
>> >> Is there a way to centre text within the text frame? I can change the
>> font
>> >> and size...but don't know how to centre it?
>> >
>> > Ok, I wanted to know it myself, and found
>> >
>> > https://stackoverflow.com/questions/42560585/how-do-i-center
>> -text-in-the-tkinter-text-widget
>>
>> Well done Peter. I've looked for ways to do this
>> (and other format tricks) several times and
>> failed. I never thought of looking at tags though...
>>
>> Using the string methods as I suggested relies on
>> using monospace fonts, and splitting the text
>> manually into lines of appropriate length - a major
>> pain.
>>
>> This works with proportional fonts and long text too.
>> So much better.
>>
>> Thanks again for your superior google skills :-)
>>
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
--
**Disclaimer: *Whilst every attempt has been made to ensure that material
contained in this email is free from computer viruses or other defects, the
attached files are provided, and may only be used, on the basis that the
user assumes all responsibility for use of the material transmitted. This
email is intended only for the use of the individual or entity named above
and may contain information that is confidential and privileged. If you are
not the intended recipient, please note that any dissemination,
distribution or copying of this email is strictly prohibited. If you have
received this email in error, please notify us immediately by return email
or telephone +61 3 5382 2529** and destroy the original message.*
More information about the Tutor
mailing list