Advertisement
gruntfutuk

groceries

Jun 1st, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. '''
  2. The task is broken down into three sections.
  3. Section 1 - User Input
  4. Section 2 - loop through the grocery list
  5. Section 3 - provide output to the console
  6. '''
  7.  
  8. groceries = []  # list of purchased groceries
  9.  
  10. while True:  # input loop
  11.    
  12.     item_name = input('Item name (or just return to exit):\n')
  13.     if not item_name:
  14.         break  # input ended, exit input loop
  15.    
  16.     while True:  # quantity input validation loop
  17.         try:
  18.             quantity = int(input('Quantity purchased:\n'))
  19.             if quantity < 1:
  20.                 raise ValueError
  21.             break
  22.         except ValueError:
  23.             print('That is not a valid entry. Please try again.')
  24.  
  25.     while True:  # price input validation loop
  26.         try:
  27.             cost = float(input('Price per item:\n'))
  28.             break
  29.         except ValueError:
  30.             print('That is not a valid entry. Please try again.')
  31.  
  32.     groceries.append({'name': item_name, 'number': quantity, 'price':cost})
  33.  
  34.  
  35. grand_total = float(0.00)
  36.  
  37. print("\n\nGroceries\n\n #\tItem      \tPrice\tTotal")
  38. for grocery in groceries:
  39.     item_total = grocery['number'] * grocery['price']  
  40.     grand_total += item_total
  41.     #Output the information for the grocery item to match this example:
  42.     #2 apple    @   $1.49   ea  $2.98
  43.     print(f"{grocery['number']:2}\t{grocery['name']:<10}\t${grocery['price']:4.2f}\t${item_total:6.2f}")
  44.  
  45. print('Grand total: $%.2f \n' %(grand_total))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement