您的位置:首页 > 编程语言 > Python开发

Python入门5-函数

2013-10-22 17:46 459 查看
由于繁忙的工作不得不把学习暂停一段落,还好,终于可以接着学习了。
def make_omelet(omelet_type):
"""This will make an omelet. You can either pass in a dictionary that
contains all of the ingredients for your omelet, or provide a string to select
a type of omelet this funcion already knows about"""
def get_omelet_ingredients(omelet_name):
"""This contains a dictionary of omelet names that can be produce and
their ingredients"""
ingredients={"eggs":2,"milk":1}
if omelet_name=="cheese":
ingredients["cheddar"]=2
elif omelet_name=="western":
ingredients["jack_cheese"]=2
ingredients["ham"]=1
ingredients["pepper"]=1
ingredients["onion"]=1
elif omelet_name=="greek":
ingredients["feta_cheese"]=2
else:
print("That's not on the menu, sorry!")
return None
return ingredients
if type(omelet_type)==type({}):
print("omelet_type is a dictionary with ingredients")
return make_food(omelet_type,"omelet")
elif type(omelet_type)==type(""):
omelet_ingredients=get_omelet_ingredients(omelet_type)
if omelet_ingredients==None:
return None
else:
return make_food(omelet_ingredients,omelet_type)
else:
# print("I don't think I can make this kind of omelet:%s" % omelet_type)
raise TypeError("No such omelet type: %s"% omelet_type)

def make_food(ingredients_needed,food_name):
"""make_food(ingredients_needed,food_name) takes the ingredients from
ingredients_needed and makes food_name"""
for ingredient in ingredients_needed.keys():
print("Adding %d of %s to make a %s"%
(ingredients_needed[ingredient],ingredient,food_name))
print("Made %s" % food_name)
return food_name
这是今天学习的函数,不错,调试出了预期结果。
本文出自 “学习小屋” 博客,请务必保留此出处http://angie.blog.51cto.com/718881/1313505
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: