Add Lab1, Problem1 and Problem2
This commit is contained in:
parent
973661fcbf
commit
5978779a9a
3 changed files with 61 additions and 0 deletions
20
thinkcspy/Chapter-12-Exercises/Problem2.py
Normal file
20
thinkcspy/Chapter-12-Exercises/Problem2.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
def add_fruit(inventory, fruit, quantity=0):
|
||||
|
||||
if fruit not in inventory:
|
||||
inventory[fruit] = quantity
|
||||
else:
|
||||
inventory[fruit] += quantity
|
||||
|
||||
return inventory
|
||||
|
||||
# make these tests work...
|
||||
new_inventory = {}
|
||||
print(new_inventory)
|
||||
add_fruit(new_inventory, 'strawberries', 10)
|
||||
# test that 'strawberries' in new_inventory
|
||||
print(new_inventory.keys())
|
||||
# test that new_inventory['strawberries'] is 10
|
||||
print(new_inventory)
|
||||
add_fruit(new_inventory, 'strawberries', 25)
|
||||
# test that new_inventory['strawberries'] is now 35)
|
||||
print(new_inventory)
|
||||
Loading…
Reference in a new issue