Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First Python Project
#4
Maybe I was not clear, but my comments were that you don't need many of these (and certainly you don't need all from my first bullet), not that you should implement them differently (this does not apply to the inefficient ones).
Let's take for example add_commas. You would keep the number and use format where you want to print. In addition f-strings that are available in 3.6+ allow for calculations. Bottom line - generally you don't want to convert the number to string just for representation (i.e. with commas)

num_items = 2500
price = 10
print('{:,d} items @ EUR {:.2f} per item --> Total EUR {:,.2F}'.format(num_items, price,num_items*price))
print(f'{num_items:,d} items @ EUR {price:.2f} per item --> Total EUR {num_items*price:,.2f}')
Output:
2,500 items @ EUR 10.00 per item --> Total EUR 25,000.00 2,500 items @ EUR 10.00 per item --> Total EUR 25,000.00
or repeat - one would always use string multiplications (if not for something else, because it is shorter) than using repeat function.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
First Python Project - by Brennan - Jun-27-2018, 04:03 AM
RE: First Python Project - by buran - Jun-27-2018, 08:03 AM
RE: First Python Project - by Brennan - Jun-27-2018, 06:33 PM
RE: First Python Project - by buran - Jun-27-2018, 07:07 PM

Forum Jump:

User Panel Messages

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