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循环,把列表中的每一项打印出来

 

names = ["Adam","Alex","Mariah","Martine","Columbus"]
# use for loop
for str in names:
    print str

 

 

 第二节

     1 介绍了我们可以使用for循环打印出字典中的每一个key

     2 比如这个例子,我们可以打印出key为foo的value值为bar

 

# A simple dictionary
d = {"foo" : "bar"}
for key in d: 
    # prints "bar"
    print d[key] 

 

 

      3 练习:打印出字典webster的所有key对应的value  

 

webster = {
	"Aardvark" : "A star of a popular children's cartoon show.",
    "Baa" : "The sound a goat makes.",
    "Carpet": "Goes on the floor.",
    "Dab": "A small amount."
}
# Add your code below!
for key in webster:
    print webster[key]

 

 

 第三节

     1 介绍了for里面我们可以添加if/else语句来判断

     2 比如

 

for item in numbers: 
    if condition: 
        # Do something

 

     3 练习:只输出列表中的7个数

 

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for num in a:
    if(a < 7):
        print a

 

 

 

 第四节

     1 介绍了在函数里面使用for循环

     2 练习

        1 写一个函数名为fizz_count,参数为列表x 

        2 函数返回列表中值为"fizz"的个数

 

# Write your function below!
def fizz_count(x):
    sum = 0
    for str in x:
        if(str == "fizz"):
            sum = sum+1
    return sum

 

 

 

 第五节

     1 练习:把以下的对应关系建立成字典prices

 

# the key and values
"banana": 4
"apple": 2
"orange": 1.5
"pear": 3
# you code here
prices = {}
prices["banana"] = 4
prices["apple"] = 2
prices["orange"] = 1.5
prices["pear"] = 3

 

 

 第六节

     1 练习:按照以下的格式输出

 

item
price: x
stock: x
# such as
apple
price: 2
stock: 0

 

 

# the first dictionary
prices = {}
prices["banana"] = 4
prices["apple"] = 2
prices["orange"] = 1.5
prices["pear"] = 3
# the second dictionary
stock = {}
stock["banana"] = 6
stock["apple"] = 0
stock["orange"] = 32
stock["pear"] = 15
# you code here
for key in prices:
    print key
    print "price: "+prices[key]
    print "stock: "+stock[key]

 

 

 第七节

 

     1 练习

        1 创建一个列表名叫groceries,有三个值分别为"banana","orange",  "apple"

        2 写一个函数名叫compute_bill,参数是列表food

        3 利用循环计算出food中所有物品的总价格

 

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:
        sum = sum + prices[str]
    return sum

 

 

 

 第八节

     1 练习

 

        1 创建一个列表名叫groceries,有三个值分别为"banana","orange",  "apple"

        2 写一个函数名叫compute_bill,参数是列表food

        3 利用循环计算出food中所有物品的总价格,但是我们在求价格的时候我们一个先判断在stock字典中对应的值是否大于0,如果是的话才进行求和并且把stock对应的value值减一

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


 

 


 

posted on 2013-09-25 20:56  you Richer  阅读(232)  评论(0编辑  收藏  举报