[Tutor] Times Tables Program that constantly tells you that you are wrong!
Matthew Polack
matthew.polack at htlc.vic.edu.au
Thu Aug 23 20:47:52 EDT 2018
Thanks Alan and Peter for all your help with this.
I got it all to work..thank you..
like learning a musical instrument...probably just need some time
consolidating the current skill set...has been a lot to take in...
but getting there steadily...and bit by bit it is starting to make sense..
I'll probably stick with the traditional %. method too for a while because
that is now what I understand.
thanks...will keep trying things..and then also reading through the various
docs provided...+ your emails.
Thank you.
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 Thu, Aug 23, 2018 at 10:34 PM, Alan Gauld via Tutor <tutor at python.org>
wrote:
> On 23/08/18 06:10, Matthew Polack wrote:
>
> > I'm also trying to solve the rounding issue...but can't work out the
> syntax
> > using the example provided...have tried this but I get an error...
> >
>
> > def viewPercent():
> > percentCalc = score/total*100
> > percentViewLab["text %.2f"] % percentCalc
>
> OK, the problem here is mixing up your data with TKinter's
> (inherited from Tcl/Tk) mechanism for accessing widget
> attributes.
>
> percentViewLab["text %.2f"]
>
> This tells Tkinter to fetch an attribute of your widget
> called "text %.2f". Of course there is no such attribute,
> it is called just "text".
>
> percentViewLab["text %.2f"] % percentCalc
>
> This tries to insert the percentCalc value into the
> string returned by the widget.
>
> Again that's not what you want. You want to insert
> the data into a string which will then be assigned
> to the widget's text attribute.
>
> val = "%.2f" % percentCalc # eg. -> val = "0.76"
>
> Now insert val into your widget
>
> percentViewLab["text"] = val
>
> or equivalently:
>
> percentViewLab.config("text", val)
>
> You can of course combine all of that with
>
> percentViewLab["text"] = ".2f" % percentCalc
>
> Personally I tend to separate the creation of the
> string from the widget assignment because it makes
> it easier to debug by printing the string to the
> console.
>
> One final note. When using % to inject data into
> a format string you MUST put the percent immediately
> after the format string. No commas or parentheses
> allowed.
>
> The % formatting style is preferred by old school
> programmers (like me) who came from the world of C and
> its relatives because C uses a very similar style in
> its printf() family of functions. However, new programmers
> may find the format() method of a string more obvious.
> (I'm thinking about your students here)
>
> Using format your case would look like:
>
> val = "{:.2f}".format(percentCalc)
>
> And the previous example would be:
>
> fail_str = """
> Sorry, you got it wrong,
> the correct answer was {:d}
> Your current score is: {:f}""".format(answer,score)
>
> It is quite similar except the placemarkers are {}
> and you call the format() method. The formatting
> characters inside the {} are different too - you
> need to read the docs... There are zillions of examples.
> You might find it more logical.
>
> --
> 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