Re: [Tutor] Times Tables Program that constantly tells you that you are wrong!
On 17/08/18 03:19, Matthew Polack wrote: > def viewPercent(): > percentCalc = score/total*100 > rounded = round(percentCalc, 2) > percentViewLab["text"] = rounded Since you only want the rounded value for display this is usually achieved using string formatting: >>> s = "Here is a string with a rounded float: %.2f" % 42.3456789 >>> s 'Here is a string with a rounded float: 42.35' That doesn't change the value of the variable but changes how it is displayed. There are lots of other options in format strings to control justification, padding etc. You should definitely explore their capabilities. Just because you are using a GUI rather than a console doesn't mean the string methods are any less useful. > from tkinter import * > import random > > # GLOBAL VARIABLES > # Created with a starting value. > answer = 0 > score = 0 > wrong = 0 > mistakes = 0 > total = 0 > > def makeproblem(): > 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 > > center = txt.tag_config("center", justify="center") > > txt.insert(0.0, sentence, "center") > txt.insert(2.2, number1, "center") > txt.insert(3.3, " x ", "center") > txt.insert(4.4, number2, "center") All of the above could be replaced with a single format string and a single insert. display = """ Here is your problem: %d x %d """ % (number1, number2) txt.insert(1.0,display, "center") > def checkanswer(): > txt.delete(0.0, 'end') > > global score > global mistakes > global total Its conventional (although not necessary) to put all globals at the very top of the function. > response = int(answerbox.get()) > wordScore = "Your score is now " > if response == answer: > score += 1 > total += 1 > result = "Great Job! " > root.update() > viewSC() > viewPercent() > viewTotal() > else : > total += 1 > result = "Sorry...you made a mistake. \n " > # the \n above adds a line break. > mistakes += 1 > viewMistakes() > viewTotal() > viewPercent() Notice you display Total and Percent in both block but do it in an inconsistent order. If you took both of this display calls outside the if/else you only need to call them once and they will be in the same sequence for all cases. > center = txt.tag_config("center", justify="center") > txt.insert(0.0, result, "center") > txt.insert(3.0, wordScore, "center") > txt.insert(8.1, score, "center") ># txt.insert(1,1, "Score is") > #txt.insert(3,3, score) Again this could all be replaced with string formatting and a single insert() > def about(): > txt.delete(0.0, 'end') > > instructions = "Here is how you play the game. Press generate > problem..and then enter your answer. Your score will be displayed below." If you use triple quoted strings you can have more text and lay it out using line breaks etc. > txt.insert(0.0,instructions) > root = Tk() > root.geometry("640x700+0+0") > root.title("Times Tables Game") > > # MENU SECTION > # These are included as an example menu structure...in many cases they > don't do much...but do feature instructions and a quit feature. > > # create a toplevel menu > menubar = Menu(root) > > # Just an example of printing hello to console for use in a menu item. > def hello(): > print ("hello!") > > # display the menu > root.config(menu=menubar) > > # create a pulldown menu, and adds it to the menu bar > filemenu = Menu(menubar, tearoff=0) > filemenu.add_command(label="Open", command=makeproblem) > filemenu.add_command(label="Save", command=makeproblem) > filemenu.add_separator() > filemenu.add_command(label="Exit", command=root.quit) > > menubar.add_cascade(label="File", menu=filemenu) > > # create more pulldown menus > editmenu = Menu(menubar, tearoff=0) > editmenu.add_command(label="Cut", command=checkanswer) > editmenu.add_command(label="Copy", command=checkanswer) > editmenu.add_command(label="Paste", command=checkanswer) > menubar.add_cascade(label="Edit", menu=editmenu) > > helpmenu = Menu(menubar, tearoff=0) > helpmenu.add_command(label="About", command=about) > menubar.add_command(label="Hello", command=hello) > menubar.add_cascade(label="Help", menu=helpmenu) > menubar.add_command(label="Quit", command=root.quit) A bit odd adding a command after you add the cascade. Normally we do the cascade as the last item. > # Plain text labels at top of window. > 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) > > # Makes an entry box with the variable of
Re: [Tutor] Times Tables Program that constantly tells you that you are wrong!
On 17/08/18 03:19, Matthew Polack wrote: > 1.) Centre feature > > When I use txt.insert I need to use the feature Peter found... > > center = txt.tag_config("center", justify="center") > txt.insert(0.0, result, "center") > > but in the Enter label section...I could just use it straight away..without > defining it with the tag... > > answerbox = Entry(root, bg="grey", font=("arial", 24, "bold"), justify > ="center") > answerbox.grid(row=15, columnspan=2, padx=0, pady=0, sticky=EW) > > Can you explain why? That's how the Tcl/Tlk designers decided to do things. tags are a concept that only apply to the Text widget so don't make much sense in a single line Entry. But essentially its an arbitrary design decision taken 30 years ago and we are stuck with it :-) > 2.) When inserting the wording re: 'You have made mistake' or 'Great job' > etc...I found I had to break it up into separate lines...as I was getting > syntax errors. > > txt.insert(0.0, result, "center") > txt.insert(3.0, wordScore, "center") > txt.insert(8.1, score, "center") > > > Is there a way to simply combine all these commands with one txt.insert and > using commas? Not with commas but you can use line breaks inside a triple quoted string and just do a single insert. In fact one of the improvements I'd suggest for your code is to use string formatting to insert the values into your strings before inserting them. eg: fail_str = """ Sorry, you got it wrong, the correct answer was %d Your current score is: %f""" % answer,score txt.insert(1.0, fail_str, "center") > 3.) Is there anything else in this code that looks like a bit of a > glaring...'Coding Rookie' mistake? Or is simply inefficient? I'll send a second reply with inline comments. -- 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] Recommended way for end users to run our Python programs?
On 17/08/18 05:40, Matthew Polack wrote: > Does this always require Python being installed as a full language on the > end users computer? No. It does require the Python interpreter plus any modules you write or use(including any modules your modules use...) There are a few tools around that can collect this information and build an executable "exe" file that bundles everything together for convenience. py2exe being the best known. The downside of this is that if you install several such programs you wind up installing multiple copies of Python which is a bit of a waste of space - but disk space is cheap nowadays... > Is there some other way to get it working in either a browser...or as a > 'self contained' windows app ...or an Android/IOS appor a regular > Windows .exe file to install? For Android there is a toolset called Kivvy that some have used successfully. Personally I use the QPython IDE on Android but it only supports CLI programs, no GUI. (I think it can run Kivvy code too) I have no idea about Python on iOS... On the other hand requiring Python on the target platform is not such an unusual thing. For many years VisualBasic programs required a VBRUN.DLL to be installed. And Java programs require the JVM to be in place. So you can just build a Windows installer that checks if Python is already there and if not installs Python and then adds your code. HTH -- 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
[Tutor] Recommended way for end users to run our Python programs?
Hi, We're enjoying learning Python in our school...but I have a question regarding the way the end users should ideally run the software. Does this always require Python being installed as a full language on the end users computer? ie. At the moment we install Python...add it to the path in Windows...develop and run the software we make...workable for the developer.but a bit of mucking around for someone who goes to use our programs. Is there some other way to get it working in either a browser...or as a 'self contained' windows app ...or an Android/IOS appor a regular Windows .exe file to install? Just trying to get my head around the 'end game' of all this? Thanks, Matt If we make a python.py program and want people on Windows to run it...does this mean there must be Python installed on that computer in order to run it. Matthew Polack | Teacher [image: Emailbanner3.png] Trinity Drive | PO Box 822 Horsham Victoria 3402 p. 03 5382 2529 m. 0402456854 e. matthew.pol...@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.* ___ 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!
Thanks Alan and Peter, I've gone through and made a bunch of changes based on all your advice. Thank you...everything seems to be fully working and I think I have made the code more efficient using your advice...hope I got everything. (full code below) I've just added an example menu to highlight the concept for teachers or students to refer to. 2 questions: 1.) Centre feature When I use txt.insert I need to use the feature Peter found... center = txt.tag_config("center", justify="center") txt.insert(0.0, result, "center") but in the Enter label section...I could just use it straight away..without defining it with the tag... answerbox = Entry(root, bg="grey", font=("arial", 24, "bold"), justify ="center") answerbox.grid(row=15, columnspan=2, padx=0, pady=0, sticky=EW) Can you explain why? 2.) When inserting the wording re: 'You have made mistake' or 'Great job' etc...I found I had to break it up into separate lines...as I was getting syntax errors. txt.insert(0.0, result, "center") txt.insert(3.0, wordScore, "center") txt.insert(8.1, score, "center") Is there a way to simply combine all these commands with one txt.insert and using commas? 3.) Is there anything else in this code that looks like a bit of a glaring...'Coding Rookie' mistake? Or is simply inefficient? Thanks again for all your help. I've found things are starting to make a lot more sense...and googling things now let me add other features. eg. The rounding function here: def viewPercent(): percentCalc = score/total*100 rounded = round(percentCalc, 2) percentViewLab["text"] = rounded Thank you Peter and Allan..and others here who have helped on the journey. from tkinter import * import random # GLOBAL VARIABLES # Created with a starting value. answer = 0 score = 0 wrong = 0 mistakes = 0 total = 0 def makeproblem(): # access global answer...this is only required IF you need to update the variable within the function. # this happens below when answer is changed to number1 * number2 global answer # erases all text in current text box. txt.delete(0.0, 'end') sentence = "Here is your problem " # example of generating a random number. number1 = random.randint(2,12) number2 = random.randint(2,12) answer = number1 * number2 # creates a justification command called centre. (Not too sure why this is needed here...but not on the answer box elsewhere!) center = txt.tag_config("center", justify="center") txt.insert(0.0, sentence, "center") txt.insert(2.2, number1, "center") txt.insert(3.3, " x ", "center") txt.insert(4.4, number2, "center") def checkanswer(): txt.delete(0.0, 'end') # as each variable changes outside of function...we need to make these global. global score global mistakes global total # checks for what is written in answerbox using the 'get' feature...makes it an integer. response = int(answerbox.get()) wordScore = "Your score is now " if response == answer: score += 1 total += 1 result = "Great Job! " root.update() viewSC() viewPercent() viewTotal() else : total += 1 result = "Sorry...you made a mistake. \n " # the \n above adds a line break. mistakes += 1 viewMistakes() viewTotal() viewPercent() center = txt.tag_config("center", justify="center") txt.insert(0.0, result, "center") txt.insert(3.0, wordScore, "center") txt.insert(8.1, score, "center") # txt.insert(1,1, "Score is") #txt.insert(3,3, score) def about(): txt.delete(0.0, 'end') instructions = "Here is how you play the game. Press generate problem..and then enter your answer. Your score will be displayed below." txt.insert(0.0,instructions) root = Tk() root.geometry("640x700+0+0") root.title("Times Tables Game") # MENU SECTION # These are included as an example menu structure...in many cases they don't do much...but do feature instructions and a quit feature. # create a toplevel menu menubar = Menu(root) # Just an example of printing hello to console for use in a menu item. def hello(): print ("hello!") # display the menu root.config(menu=menubar) # create a pulldown menu, and adds it to the menu bar filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Open", command=makeproblem) filemenu.add_command(label="Save", command=makeproblem) filemenu.add_separator() filemenu.add_command(label="Exit", command=root.quit) menubar.add_cascade(label="File", menu=filemenu) # create more pulldown menus editmenu = Menu(menubar, tearoff=0) editmenu.add_command(label="Cut", command=checkanswer) editmenu.add_command(label="Copy", command=checkanswer) editmenu.add_command(label="Paste", command=checkanswer) menubar.add_cascade(label="Edit", menu=editmenu) helpmenu = Menu(menubar, tearoff=0) helpmenu.add_command(label="About", command=about)