前言
A Day at the Supermarket该项目用来回顾/强化之前学习Python的知识点。
names = ["Adam","Alex","Mariah","Martine","Columbus"]
for elements in names:
print elements
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]
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
"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 count
fizz_count(["fizz","cat","fizz"])
for letter in "Codecademy":
print letter
# Empty lines to make the output pretty
print
print
word = "Programming is fun!"
for letter in word:
# Only print out the letter i
if letter == "i":
print letter
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
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]
total = 0
for key in prices:
print "prices: %s" % prices[key]*stock[key]
total += prices[key]*stock[key]
print total
groceries = ["banana", "orange", "apple"]
def compute_bill(food):
total = 0
for item in food:
total += prices[item]
stock[item] -= 1
return total
def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total += prices[item]
stock[item] -= 1
return total
for
loops with lists and dictionariesstock
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