Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. def get_number(question, condition, error):
  2.     number = 0
  3.     while True:
  4.         try:
  5.             number = int(raw_input(question + "\n"))
  6.         except ValueError:
  7.             print
  8.             print 'Input must be numerical.'
  9.             print
  10.             continue
  11.            
  12.         if condition(number):
  13.             break
  14.         else:
  15.             print
  16.             print error
  17.             print
  18.            
  19.     return number
  20.  
  21. # Get the amount of ingredients.
  22. amount = get_number('How many ingredients?', \
  23.                     lambda x: x > 0, \
  24.                     'Amount must be greater than 0.')
  25.    
  26. # Get the ingredient info and the total cost.
  27. total_cost = 0
  28. for i in xrange(1, amount + 1):
  29.     prefix = '#' + str(i) + ': '
  30.    
  31.     stored = get_number(prefix + 'How many do you have?', \
  32.                         lambda x: x >= 0, \
  33.                         'Surely you cannot have a negative amount.')
  34.     needed = get_number(prefix + 'How many do you need?', \
  35.                         lambda x: x >= 0, \
  36.                         "You have more somewhere, I'm sure of it.")
  37.     cost   = get_number(prefix + 'How much does one cost?', \
  38.                         lambda x: x > 0, \
  39.                         "That's a deal too good to be true!")
  40.    
  41.     if stored < needed:
  42.         total_cost += cost * (needed - stored)
  43.  
  44. # Print the result.
  45. print "Total cost: " + str(total_cost)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement