Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_number(question, condition, error):
- number = 0
- while True:
- try:
- number = int(raw_input(question + "\n"))
- except ValueError:
- print
- print 'Input must be numerical.'
- print
- continue
- if condition(number):
- break
- else:
- print
- print error
- print
- return number
- # Get the amount of ingredients.
- amount = get_number('How many ingredients?', \
- lambda x: x > 0, \
- 'Amount must be greater than 0.')
- # Get the ingredient info and the total cost.
- total_cost = 0
- for i in xrange(1, amount + 1):
- prefix = '#' + str(i) + ': '
- stored = get_number(prefix + 'How many do you have?', \
- lambda x: x >= 0, \
- 'Surely you cannot have a negative amount.')
- needed = get_number(prefix + 'How many do you need?', \
- lambda x: x >= 0, \
- "You have more somewhere, I'm sure of it.")
- cost = get_number(prefix + 'How much does one cost?', \
- lambda x: x > 0, \
- "That's a deal too good to be true!")
- if stored < needed:
- total_cost += cost * (needed - stored)
- # Print the result.
- print "Total cost: " + str(total_cost)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement