Python 入门教程 9 ---- A Day at the Supermarket
第一节
1 介绍了for循环的用法
for variable in values:
statement
2 for循环打印出列表的每一项
for item in [1 , 2 , 3]:
print item
那么将打印出1,2,3
3 练习:使用for循环,把列表中的每一项打印出来
shopping_list = ["banana", "orange", "apple"]stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15} prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3}# Write your code below!def compute_bill(food): sum = 0 for str in food: if(stock[str] > 0): sum = sum + prices[str] stock[str] = stock[str]-1 return sum