Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- The task is broken down into three sections.
- Section 1 - User Input
- Section 2 - loop through the grocery list
- Section 3 - provide output to the console
- '''
- groceries = [] # list of purchased groceries
- while True: # input loop
- item_name = input('Item name (or just return to exit):\n')
- if not item_name:
- break # input ended, exit input loop
- while True: # quantity input validation loop
- try:
- quantity = int(input('Quantity purchased:\n'))
- if quantity < 1:
- raise ValueError
- break
- except ValueError:
- print('That is not a valid entry. Please try again.')
- while True: # price input validation loop
- try:
- cost = float(input('Price per item:\n'))
- break
- except ValueError:
- print('That is not a valid entry. Please try again.')
- groceries.append({'name': item_name, 'number': quantity, 'price':cost})
- grand_total = float(0.00)
- print("\n\nGroceries\n\n #\tItem \tPrice\tTotal")
- for grocery in groceries:
- item_total = grocery['number'] * grocery['price']
- grand_total += item_total
- #Output the information for the grocery item to match this example:
- #2 apple @ $1.49 ea $2.98
- print(f"{grocery['number']:2}\t{grocery['name']:<10}\t${grocery['price']:4.2f}\t${item_total:6.2f}")
- print('Grand total: $%.2f \n' %(grand_total))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement