[Tutor] Times Tables Program that constantly tells you that you are wrong!

Matthew Polack matthew.polack at htlc.vic.edu.au
Tue Aug 14 20:56:55 EDT 2018


Hi,

A small group of students and I are making steady progress learning Python.
Thanks for the people here who have helped us!

I have an problem with my simple 'Times Tables Program'.

The 'Make Problem' function works fine...it can generate a random problem
and place it in a variable called "answer"...which I've made global because
it needs to be used in the 'Check answer' function.

The trouble is the program is unable to tell you that you are right....even
when you type in the correct answer.

Here is the full code:

from tkinter import *

import random

answer = "global"

def makeproblem():
# deletes anything in text box from location 00 to the end
txt.delete(0.0, 'end')
sentence = "Your first problem is "

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')

# checks for what is written in answerbox
response = int(answerbox.get())
if response == answer:
result = "Great Job!"
score = int(score + 1)
else :
result = "Sorry...you were wrong"
txt.insert(0.0, result)
txt.insert(1,1, "Score is")
txt.insert(2,2, score)

root = Tk()
root.geometry("640x640+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=10, wrap=WORD, font=("arial", 24, "bold"))
txt.grid(row=12, columnspan=2, sticky=EW )

root.mainloop()


*Question 1:*

*Why cant the program check if 'answer' variable is correct?*

lib\tkinter\__init__.py", line 3269, in insert
    self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: bad text index "1"

*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?

txt = Text(root, width=35, height=10, wrap=WORD, font=("arial", 24, "bold"))
txt.grid(row=12, columnspan=2, sticky=EW )


*Question 3:*

My 'Score' check idea..may not actually work...haven't been able to get
that far yet though!

Thanks so much for any clues.

- 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

-- 
**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