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

2018-08-16 Thread Alan Gauld via Tutor
On 16/08/18 08:18, Matthew Polack wrote:

> 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%.

> def percentCheck():
> global percentScore
> global score
> global mistakes
> global total

While you do need some globals in your program you only
need to declare the ones you are changing. In this case
you only need to declare percentScore as global.

> percentScore = float(score/total) * 100

To debug this I would add a print line such as

print(score, total)

Since if you always get 100 it suggests that score
and total are the same.

One problem here is that you are converting to float
after doing the division. That's usually the wrong
thing to do. In fact I notice you do that a lot of
conversions on expressions. Its much safer to do the
conversions on the individual values then perform
the computation after the conversion.

In fact in this case (and most of the other similar
cases) you should not need the float conversion since
you already converted the values when you read them
from the GUI. You should aim to convert the data
into the correct type as soon as possible then you
will not need to use conversions anywhere else.

Some more observations below...

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

> else :
> score= int(score + 1)
> total = int(total + 1)

Notice that regardless of what the answer is
you update both score and total. And again you
don't need the int() conversions, the variables
are already integers.

> root = Tk()
> root.geometry("640x700+0+0")
> root.title("Times Tables")
> 
> 
> #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 )

AS per Peter's link you need to create a tag and configure
it with the font and justification you want. Then when you
insert the text you add the tag name.

eg:

center = txt.tag_config("center", justify="center")
txt.insert(1.0, "Hello world", "center")


> blankline3 = Label(root, text = "", bg = "white")
> blankline3.grid(row=17, sticky='ew')

I'm not sure what this is for. If you want some space
just add some padding to the label below.

> scorelab = Label(root, text="Score", bg="green", fg="white", font=("arial",
> 20, "bold"))
> scorelab.grid(row=18, column=0, sticky=E)

Note that you create a Label here, but in ViewSC below you create
another label which causes this one to be deleted. This is not
necessary, instead just change the text of this label in viewSC.

scorelab['text'] = "New text here"

> totalLab = Label(root, text="Out of", bg="purple", fg="white",
> font=("arial", 20, "bold"))
> totalLab.grid(row=19, column=0, sticky=E)

Same applies to the other viewXXX functions, just update these
labels, don't build and destroy new label widgets each time.
That's a lot of wasted work and time for your PC.

> def viewSC():
> global score

You don't change score so no need for global here.
You only need global when changing the data.

> scoreViewLab = Label(root, text=score, fg="black", bg="grey",
> font=("arial", 20, "bold"))
> scoreViewLab.grid(row=18, column=1, sticky=W)

I just noticed this is in a different grid location,
but the principle still applies create it once at the
start of the GUI then update it in the view functions.

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

You declare wrong as global but never use it.
Should this function not be called viewMistakes?

> def viewPercent():
> global percentScore
> global total
> 

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

2018-08-16 Thread Peter Otten
Matthew Polack wrote:

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

You increment the score no matter whether the answer is correct or not.

> if response == answer:
> score = int(score + 1)

> else :
> score= int(score + 1)

Random remarks

- Your code suffers from "over-globalisation". You only need to declare a 
variable global when you want to change it from within a function. Example:

x = 0

def show_x():
   # no global necessary
   print("x =", x)

def inc_x():
global x
x += 1

- You create a new Label in every call of viewXXX(). For long-running 
applications that will consume a lot of memory. You should instead create 
the label once and then update it

percentViewLab = Label(...)
percentViewLab.grid(...)

def viewPercent():
percentViewLab["text"] = percentScore

- You convert integers to integers.

>score = int(score + 1)

should better be written

 score = score + 1

or
 score += 1

- I rarely say that, but you have too many functions -- or rather you have 
written them in such a way that you need to call them in a special order. 
Personally I would omit the percentCheck function and the percentScore 
variable and rewrite viewPercent as

def viewPercent():
percentViewLab["text"] = score/total*100

or if you want to keep the function

def get_percent_score():
return score/total*100

def viewPercent():
percentViewLab["text"] = get_percent_score()

With both approaches viewPercent() can never show an out-of-date value.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need PYTHON, MySQL, WEB2PY OR DJANGO Tutor

2018-08-16 Thread Alan Gauld via Tutor
On 15/08/18 18:45, Hamid Nehoray via Tutor wrote:
> Greetings,

Greetings, welcome to the tutor list.

> I'm an old programmer who has some old ERP systems...
> I need somebody to teach me the new ways using Python, 

For anyone who can already program I always recommend
starting with the official Python tutorial:

http://docs.python.org/tut/


> MySql 

There is a MySQL tutorial on their web site, but
I've never used it. However, if you are familiar
with any major SQL database(Oracle, DB2, etc)
then you should pick it up easily.

> and Web2Py or Django.  

Django has some excellent tutorials although I usually
recommend watching a few YouTube videos to get a feel
for things first. Then dive into the detail with a
written tut. but you will need to have completed
the Python tutorial(above0 first.

A know nothing about Web2py...

> I'm located in Los Angeles.Regards,Hamid Nehoray.

The tutor list works by answering your questions
as you post them to the list. Please always use
plain text to retain formatting. Always post the
full text of error messages not just a summary.
And tell us the OS and Python versions.

-- 
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@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-08-16 Thread Matthew Polack
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