Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops maybe
#1
I'm a beginner and I'm struggling with the following task:

Write a program that will add up the cost of several items. The program should ask the user to enter how many items there are, and then ask for the price of each item with the prompt message in the form “Please enter the price of the item: £”. The user enters the price of each item as a number without a currency symbol. The program should add the prices together and then display the total cost with a user-friendly message, including the currency symbol, £.

Challenge: Develop your program further so that the user enters the amount that they have paid (e.g. 20.00 without the £ symbol) and the program displays how many £20, £10, £5 notes and how many £2, £1, 50p, 20p, 10p, 5p, 2p and 1p coins should be given as change, or how much more money is needed.

For the above challenge, the // operator (floor division, or integer division) and % modulo operator (gives the positive remainder from integer division) are helpful.

e.g.

5 // 2 = 2 (the integer part of the division calculation)

5 % 2 = 1 (the remainder from integer division)

So far I have...

# Adding items
print (" How many items do you have?")
numberofitems = input()

item_1 = float(input('Enter the first items price £: '))
item_2 = float(input('Enter the second items price £: '))

print(item_1 + item_2)
How do I make the users input, the number of times the program asks for the price?

Any help would be greatly appreciated thanks
Reply
#2
Loops are the exact way to go - I would recommend a for loop for this (although a while loop would work just as well).
Have a read on for loops here
If you are really stuck, here is a starting point.
Reply
#3
print (" How many items do you have?")
numberofitems = input()
total = 0 
for item in range(numberofitems):
    x = float(input('Enter the price £: '))
    total = total + x
#now print out the total in a nice format
This creates a loop that asks for the price of each object, you will then need to add the code for formatting the sum (total)
Reply
#4
(Dec-02-2019, 02:58 AM)jefsummers Wrote:
print (" How many items do you have?")
numberofitems = input()
total = 0 
for item in range(numberofitems):
    x = float(input('Enter the price £: '))
    total = total + x
#now print out the total in a nice format
This creates a loop that asks for the price of each object, you will then need to add the code for formatting the sum (total)
numberofitems = input()
shouldn't that be
int(input())
?
Reply
#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


Forum Jump:

User Panel Messages

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