Python Forum
tip calculator not displaying proper dollar format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tip calculator not displaying proper dollar format
#1
Howdy y'all, Smile

I am working from a self-teach book and at the end of chapter 2 I have been given the challenge of creating a tip calculator. It actually says: "Write a tipper program where the user enters a restaurant bill total. The program should then display two amounts: a 15% tip and a 20% tip.
In trying to challenge myself further I have expanded slightly on the request by calculating the sales tax and offering a third option
of a 10% tip.
I have the app working... acceptably with the exception that I can't get my calculations to display in typical 0.00 (dollar) format.
I have searched the internet and youtube for examples and even found a couple, however when I tried to apply them, I ended up with syntax errors. I suspect that I am jumping the gun and trying to do something more advanced than the book intended although it hasn't covered formatting yet, I'm not sure how this was supposed to work without it. I'm probably trying to make something relatively simple much more complicated than it needs to be, as usual. Blush
I would greatly appreciate it if someone could offer me a suggestion or two to fix this issue.
Thanks!

food = float(input('What is your food total?: '))

tax = 0.08 * food 
print('The sales tax is: $', tax)  

total = tax + food
print('Your total bill comes to: $', total)


tip10 = 0.10 * total
tip15 = 0.15 * total
tip20 = 0.20 * total
print('To tip 10% the amount is: $', tip10)
print('To tip 15% the amount is: $', tip15)
print('To tip 20% the amount is: $', tip20)
And here is an example of my output:

Output:
What is your food total?: 56.85 The sales tax is: $ 4.548 Your total bill comes to: $ 61.398 To tip 10% the amount is: $ 6.139800000000001 To tip 15% the amount is: $ 9.2097 To tip 20% the amount is: $ 12.279600000000002 >>>
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#2
Try this

print('%.2f' % number)
Reply
#3
Use:
print('To tip 20% the amount is: $ {:.2f}'.format(tip20))
Another way is to use [url=https://docs.python.org/3/library/functions.html#round]round[/url].

print('To tip 20% the amount is: $',round(tip20,2))
Reply
#4
using the format statement should fix this:
food = float(input('What is your food total?: '))

tax = 0.08 * food
print('The sales tax is: ${0:.2f}'.format(tax))

total = tax + food
print('Your total bill comes to: ${0:.2f}'.format(total))

tip10 = 0.10 * total
tip15 = 0.15 * total
tip20 = 0.20 * total
print('To tip 10% the amount is: ${0:.2f}\nTo tip 15% the amount is:'
      ' ${1:.2f}\nTo tip 20% the amount is: ${2:.2f}'.format(tip10, tip15, tip20))
Reply
#5
(Aug-30-2017, 08:36 AM)hbknjr Wrote: Use:
print('To tip 20% the amount is: $ {:.2f}'.format(tip20))
Another way is to use [url=https://docs.python.org/3/library/functions.html#round]round[/url].

print('To tip 20% the amount is: $',round(tip20,2))

This was the answer I was looking for!! Thank you so much!!
I used both of your suggestions and they both solved the issue at hand. I was wondering though if there is a certain situation[s] where one option is preferred over the other?

I just wanted to post this to show anyone else that might have this same problem arise.

Here is the code with the new changes, I incorporated both to demonstrate that they both give the same desired result.

Thanks again to HBKNJR, and others for your help!! Smile

food = float(input('What is your food total?: '))

tax = 0.08 * food 
print('The sales tax is: $ {:.2f}'.format(tax))

total = tax + food
print('\nYour total bill comes to: $ {:.2f}'.format(total))


tip10 = 0.10 * total
tip15 = 0.15 * total
tip20 = 0.20 * total
print('\nTo tip 10% the amount is: $ {:.2f}'.format(tip10))
print('To tip 15% the amount is: $',round(tip15,2))
print('To tip 20% the amount is: $',round(tip20,2))
And the new and improved output:

Output:
What is your food total?: 56.85 The sales tax is: $ 4.55 Your total bill comes to: $ 61.40 To tip 10% the amount is: $ 6.14 To tip 15% the amount is: $ 9.21 To tip 20% the amount is: $ 12.28 >>>
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#6
Thanks for your post and answers from others. I am new to Python too and teaching myself. I learn basics of Python on Udacity. Could you tell me the name of the book you are reading? I'll appreciate it.
Reply
#7
(Aug-30-2017, 01:43 PM)syogun Wrote: Thanks for your post and answers from others. I am new to Python too and teaching myself. I learn basics of Python on Udacity. Could you tell me the name of the book you are reading? I'll appreciate it.

Sure!

I started with a very basic book called, "Coding for Beginners", by Mike McGrath. I used that as a stepping stone to see if coding was something I was even going to be interested in. The problem I had with that book is that it was not very detailed in the explanation of what it was trying to teach. To my own disappointment I only got about half way through it before I got so frustrated because most of the hands on labs in the book would give me a different output than the examples and because there was so little explanation it was very difficult to figure out where the problems were.

I then went and purchased a slightly more advanced book which I am currently using called, "Python Programming for the Absolute Beginner- third edition", by Michael Dawson. So far I am really enjoying this book and the author does a much better job of explaining the concepts and whys of what we're doing, plus I think the labs or hands on projects are better suited to real world scenarios.

Anyway, I hope this answers your question and good luck with your study! Smile
Quote:If you can't learn to do something well?... Learn to enjoy doing it poorly.
Reply
#8
(Aug-30-2017, 08:59 AM)Crackity Wrote: I was wondering though if there is a certain situation[s] where one option is preferred over the other?
"{}".format(arg) is for formatting string and printing them. You'll receive a string when using it

whereas round(number,ndigits) is used to round off, it returns float which can further be used for calculations.
Reply
#9
For currencies you can use (is not a must), locale from pythons stdlib.
Which locales you can use, depends on your operation system installed locales.

For example I can't use en_US because I haven't installed it.

import locale

locale.setlocale(locale.LC_ALL, 'de_DE')
eur = locale.currency(12.3391)
print(eur)

locale.setlocale(locale.LC_ALL, 'en_US')
dollar = locale.currency(12.3391)
print(dollar)
It does also round for you. If you take a look into the locale module.
Other modules depends also on locale. For example daytime:

{:%A}'.format(datetime.datetime.now())
Output:
'Freitag'
It's just good to know.

Yeah, awesome, we've Friday.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#10
Quote:Sure!

I started with a very basic book called, "Coding for Beginners", by Mike McGrath. I used that as a stepping stone to see if coding was something I was even going to be interested in. The problem I had with that book is that it was not very detailed in the explanation of what it was trying to teach. To my own disappointment I only got about half way through it before I got so frustrated because most of the hands on labs in the book would give me a different output than the examples and because there was so little explanation it was very difficult to figure out where the problems were.

I then went and purchased a slightly more advanced book which I am currently using called, "Python Programming for the Absolute Beginner- third edition", by Michael Dawson. So far I am really enjoying this book and the author does a much better job of explaining the concepts and whys of what we're doing, plus I think the labs or hands on projects are better suited to real world scenarios.

Anyway, I hope this answers your question and good luck with your study! Smile

Sorry for my late reply. Thank you so much for the answer! I'll search the "Python Programming for the Absolute Beginner- third edition" and give it a try.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Histogram using pandas dataframe not showing proper output ift38375 1 2,202 Jul-04-2019, 10:43 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020