[Tutor] Want to keep to two decimal places for currency
I've looked online but I'm confused - I need to keep it so that the following program limits the output to two decimal places since it deals in currency. How do I incorporate that into my current code below? The textbook I'm using doesn't describe how to do that. Thanks in advance. #Challenge Chapter 2 #Question number three print("This program helps you to determine a tip amount of either") print("15 or 20 percent.") bill=float(input("\nHow much was your restaurant bill total (in dollars)? ")) fifteen=float(bill*0.15) twenty=float(bill*0.2) print("\n15% tip = $", fifteen) print("\n20% tip = $", twenty) input("\n\nPress the enter key to exit.") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
Thank you for your replies. I suspect the solution is a bit more advanced than where I'm at now, which is chapter 2 of a beginner's book. Not sure why the author chose to use examples using money calculations when other calculations that don't need rounding would have sufficed. I will have to revisit this issue when I'm further along. On Wed, Oct 23, 2013 at 8:44 PM, Danny Yoo wrote: > > > > On Wed, Oct 23, 2013 at 4:13 PM, Alan Gauld wrote: > >> On 23/10/13 22:39, Shelby Martin wrote: >> >>> I've looked online but I'm confused - I need to keep it so that the >>> following program limits the output to two decimal places since it deals >>> in currency. >>> >> >> Its generally a bad idea to use floats when dealing with currency since >> they can introduce small errors which can accumulate over time. It's better >> to either use the Decimal module or to convert money to cents/.pennies and >> then convertback to dollars for display purposes. >> > > > Hi Shelby, > > > If you want to read some gory details, you can find the reasons for why > it's unsafe to represent currency with "floating point" numbers. There's a > classic paper called "What Every Computer Scientist Should Know About > Floating-Point Arithmetic": > > http://www.validlab.com/goldberg/paper.pdf > > and you probably don't want to read the whole thing. :P A rough gist of > the problem: traditional, engineering-focused computer math has a > particular trade-off that most programmers do not realize at first: it > trades accuracy for speed. The native hardware of your computer does > calculations in base-2 rather than base-10 arithmetic. Unfortunately, that > means that fractional quantities take a representational hit: your computer > cannot accurately represent certain decimals in base-2. > > It turns out that this floating point arithmetic limitation is not so bad > for a lot of important applications. But it doesn't bode well at all for > applications that deal with money. There are libraries in Python that > avoid using the built-in floating point hardware, such as the Decimal > library that Alan noted. Computations with it are slower, but that's an > acceptable price for getting the dollars and cents right. If we would do > this sort of thing in the real world, we'd use something like the Decimal > library. You'll see equivalent kinds of libraries in other programming > languages, like the BigDecimal class in Java. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about conditions and empty values
For the program below, if I enter "0" at the prompt, it provides the reply "Please, sit. It may be a while". However, if I just press the Enter key, it shuts the program down without a reply from the Maitre D'. My question is this - the author of this exercise states the condition is False if either zero or "empty" is the value. I'm assuming he means that empty is just pressing Enter without entering a number? He talks about testing for empty values, but I'm not seeing that entering an empty value here yields the response he is talking about. Unless I'm mistaken about something. Thanks in advance for your assistance! #Maitre D' #Demonstrates treating a value as a condition print("Welcome to the Chateau D' Food") print("It seems we are quite full this evening.\n") money = int(input("How many dollars do you slip the Maitre D'?")) if money: print("Ah, I am reminded of a table. Right this way.") else: print("Please, sit. It may be a while.") input("\n\nPress the enter key to exit.") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about conditions and empty values
I use the Python GUI called IDLE. As far as I can tell, it doesn't show me any error messages, but when the program shuts down suddenly due to a coding error, some sort of text pops up very briefly on the screen before it shuts down. Sorry, I'm a complete newbie to Python :) And thanks for your help. On Sat, Oct 26, 2013 at 4:45 PM, Alan Gauld wrote: > On 26/10/13 20:13, Shelby Martin wrote: > > My question is this - the author of this exercise states the condition >> is False if either zero or "empty" is the value. I'm assuming he means >> that empty is just pressing Enter without entering a number? >> > > Normally that would be correct but... > > > >> money = int(input("How many dollars do you slip the Maitre D'?")) >> > > Here we try to convert the string to an int. and int() fails > when given an empty string so your program never reaches the > if test. > > Which begs the question: GHOw are you running your programs? > If you used a console or an IDE it should have shown you the error message > which would have explained what and where things went wrong. > > You would need to either use a try/except clause around the conversion or > check for an empty string before converting. try/except is the preferred > route but you may not have covered that yet. > > > if money: >> print("Ah, I am reminded of a table. Right this way.") >> else: >> print("Please, sit. It may be a while.") >> > > If you did get the error message then please, in future, include > any such in their entirety in posts because they greatly simplify > diagnosing more complex issues. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/**alangauldphotos<http://www.flickr.com/photos/alangauldphotos> > > __**_ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/**mailman/listinfo/tutor<https://mail.python.org/mailman/listinfo/tutor> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor