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
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
相关文章推荐
- python的函数---python入门学习(三)
- Python基础入门(十七)-函数里的参数
- Python爬虫入门之request函数定制
- python 快速入门函数
- Python 3.x 入门教程 函数
- Python 3从入门到精通16-常用函数
- Python入门之函数
- Python入门篇之函数
- Python 3 语法小记(一)入门 (print 函数用法总结)
- 编程入门python之定义函数【转】
- python入门----参数:函数,字符串
- python3入门之函数
- python中的函数用法入门教程
- 函数:内嵌函数和闭包 - 零基础入门学习Python020
- Python:基本运算、基本函数及NumPy入门
- [Python入门及进阶笔记]Python-基础-函数小结
- python入门笔记--第一个函数print
- Python3 (入门3) 函数
- python入门(八) -- 函数
- python入门:关于堆操作的一个模块heapq及其函数