intro2ai/p0_tutorial/buyLotsOfFruit.py

47 lines
1.5 KiB
Python
Raw Normal View History

2021-10-12 02:45:38 +02:00
# buyLotsOfFruit.py
# -----------------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).
"""
To run this script, type
python buyLotsOfFruit.py
Once you have correctly implemented the buyLotsOfFruit function,
the script should produce the output:
Cost of [('apples', 2.0), ('pears', 3.0), ('limes', 4.0)] is 12.25
"""
fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75,
'limes':0.75, 'strawberries':1.00}
def buyLotsOfFruit(orderList):
"""
orderList: List of (fruit, numPounds) tuples
Returns cost of order
"""
2021-10-12 03:01:31 +02:00
try:
return sum([fruitPrices[fruit] * quantity
for fruit, quantity in orderList])
except KeyError:
print("Unexpected fruit!")
return None
2021-10-12 02:45:38 +02:00
# Main Method
if __name__ == '__main__':
"This code runs when you invoke the script from the command line"
orderList = [ ('apples', 2.0), ('pears', 3.0), ('limes', 4.0) ]
2021-10-12 03:01:31 +02:00
print('Cost of', orderList, 'is', buyLotsOfFruit(orderList))