Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops maybe
#5
Without user input validation the code can be quite simple:

quantity = int(input('How many items do you have: '))
total = sum(float(input(f"Enter the price of item #{i+1}: ")) for i in range(quantity))
However, if user enters anything what can't be converted into int/float program will rise ValueError. Its upon parameters of task if one should be defensive against it.

And of course - while dealing with floats one must remember:

>>> 0.1 + 0.1 + 0.1 == 0.3
False
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Loops maybe - by gray1306 - Nov-30-2019, 08:41 PM
RE: Loops maybe - by DreamingInsanity - Nov-30-2019, 08:52 PM
RE: Loops maybe - by jefsummers - Dec-02-2019, 02:58 AM
RE: Loops maybe - by DreamingInsanity - Dec-02-2019, 10:10 AM
RE: Loops maybe - by perfringo - Dec-02-2019, 10:47 AM

Forum Jump:

User Panel Messages

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