首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Python 入门教程 九 - A Day at the Supermarket

2013-09-28 
Python 入门教程 9 ---- A Day at the Supermarket 第一节1 介绍了for循环的用法for variable in values:s

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

 


热点排行