Python 为什么for循环返回None?
我正在代码学院做一个练习,反复浏览Python 为什么for循环返回None?,python,Python,我正在代码学院做一个练习,反复浏览购物清单。 为什么以下代码结果在结果中返回额外的None shopping_list = ["banana","apple"] stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } # Write your code
购物清单
。
为什么以下代码结果在结果中返回额外的None
shopping_list = ["banana","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 x in food:
print x
total += prices[x]
compute_bill(shopping_list)
您需要使用
return
语句来获取函数compute\u bill
的结果。您的函数将返回None
。也许您的环境只是在函数运行后显示返回值#在下面编写代码!def compute_bill(food):total=0表示食品中的x:print x total+=prices[x]返回total print“total:+str(compute_bill(shopping_list)),在本教程本节的第二个示例之后-。
shopping_list = ["banana","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 x in food:
print x
total += prices[x]
return total
print(compute_bill(shopping_list))