Python学习——A Day at the Supermarket小项目
来源:互联网 发布:利驰软件电气教程 编辑:程序博客网 时间:2024/06/05 20:01
前言
A Day at the Supermarket该项目用来回顾/强化之前学习Python的知识点。
1、Before We Begin
names = ["Adam","Alex","Mariah","Martine","Columbus"]for elements in names: print elements
上述代码,使用for循环,将names这个list中的items逐个输出/打印出来——遍历。
2、This is Key!
对dictionary使用for循环,输出是无序的,即每次输出,“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]
3、Control Flow and Looping
在循环中,有选择性地输出数据
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]for item in a: if item % 2 == 0: print item
4、Lists + Functions
函数可以将List作为其输入参数,进行下一步操作运算
任务:Write a function that counts how many times the string
"fizz"
appears in a list.# Write your function below!def fizz_count(x): count = 0 for item in x: if item == "fizz": count += 1 return countfizz_count(["fizz","cat","fizz"])
5、String Looping
字符串和list类似,其每个字符相当于元素,所以字符串也可以像list一样循环操作。
可参见下述代码:
for letter in "Codecademy": print letter # Empty lines to make the output prettyprintprintword = "Programming is fun!"for letter in word: # Only print out the letter i if letter == "i": print letter
6、Your Own Store!
创建一个价格dictionary——prices
prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3}
7、Investing in Stock
创建一个库存dictionary——stock
stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15}
8、Keeping Track of the Produce
输出价格和库存信息
prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3}stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15}for key in prices: print key print "price: %s" % prices[key] print "stock: %s" % stock[key]
9、Something of Value
将每种商品的总价格及所有商品的价格计算并打印出来total = 0for key in prices: print "prices: %s" % prices[key]*stock[key] total += prices[key]*stock[key]print total
10、Shopping at the Market
groceries = ["banana", "orange", "apple"]
11、Making a Purchase
计算自己选购商品的价格
def compute_bill(food): total = 0 for item in food: total += prices[item] stock[item] -= 1 return total
12、Stocking Out
提示:If you're buying a banana, check if it's in stock (larger than zero). If it's in stock, add the cost of a banana to your bill. Finally, decrement the stock of bananas by one!
def compute_bill(food): total = 0 for item in food: if stock[item] > 0: total += prices[item] stock[item] -= 1 return total
上述已经完成list、dictionary的创建
- ——Using
for
loops with lists and dictionaries - ——Writing functions with loops, lists, and dictionaries
- ——Updating data in response to changes in the environment (for instance, decreasing the number of bananas in
stock
by 1 when you sell one). - 完整代码如下:
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): total = 0 for item in food: if stock[item] > 0: total += prices[item] stock[item] -= 1 return total
0 0
- Python学习——A Day at the Supermarket小项目
- Python学习笔记(5)——A Day at the Supermarket
- Python 入门教程 9 ---- A Day at the Supermarket
- UVALive 4370 A Day at the Races
- Day at the Race
- CF- Day at the Beach
- The First Day at School
- Second Day——python学习
- Second Day——Python 学习
- An App a day——NFC小助手
- Codeforces Round #332 (Div. 2)——C. Day at the Beach
- URAL2046 The First Day at School
- ural 2046 The First Day at School
- codeforces 599C Day at the Beach
- Codeforces 599 C. Day at the Beach
- CodeForces 599C Day at the Beach
- Codeforces 599 C Day at the Beach
- Codeforces 599C Day at the Beach
- 理解redux
- 2016年“闪婚”已创近年来新高,年轻人应该谨之慎行
- 神经网络与深度学习(4):改进神经网络的学习方法
- JAVA的随机数
- 快速判断当前应用界面属于哪个Activity
- Python学习——A Day at the Supermarket小项目
- 斐波那契数列(c++数组实现,递归实现与优化)
- 标签Tab切换 PagerSlidingTabStrip的使用
- UML简介
- x210 如何刷机
- 交通灯
- relocation section 9 uses unexpected symbol table 0
- 乐观锁和悲观锁简介
- ADSL上网方式