Python 在多个函数中使用相同的列表/字典
我是python新手,目前仍在学习hwo处理列表和字典 我有这两个功能Python 在多个函数中使用相同的列表/字典,python,python-2.7,Python,Python 2.7,我是python新手,目前仍在学习hwo处理列表和字典 我有这两个功能 def food_database(item_name, size_serv, calorie_serv, protein_serv, carb_serv, fat_serv): # used to list the different foods when users ask for it # food database food_dict = [ { 'food
def food_database(item_name, size_serv, calorie_serv, protein_serv, carb_serv, fat_serv):
# used to list the different foods when users ask for it
# food database
food_dict = [ {
'food_name': item_name,
'serving_size': size_serv,
'serving_calorie': calorie_serv,
'serving_protien': protein_serv,
'serving_fat': fat_serv,
'serving_carb': carb_serv
} ]
print food_dict
def food_in_diet(item_name, size_serv, calorie_serv, protein_serv, carb_serv, fat_serv, num_serv):
# used to show how much is in the diet plan for the user
User_diet_dict = [ {
'food_name': item_name,
'amount': num_serv*size_serv,
'serving_calorie': num_serv*calorie_serv,
'serving_protien': protein_serv,
'serving_fat': fat_serv,
'serving_carb': carb_serv
} ]
print User_diet_dict
我有另一个功能
def add_food():
ask_to_add_another = raw_input("Would you like to add another food?(y/n)")
if ask_to_add_another == 'y':
# update
item_name = raw_input("What is the name of the food you would like to add? ")
size_serv = input("What is the size(grams) in each serving of %s? " % item_name)
calorie_serv = input("How many calories is in each serving of %s? " % item_name)
protein_serv = input("How many grams of protein is in each serving of %s? " % item_name)
carb_serv = input("How many grams of carbohydrates is in each serving of %s? " % item_name)
fat_serv = input("How many grams of fat is in each serving of %s? " % item_name)
num_serv = input("How many servings of %s would you like to add? " % item_name)
food_dict.append( {
'food_name': 'item_name',
'serving_size': size_serv,
'serving_calorie': calorie_serv,
'serving_protien': protein_serv,
'serving_fat': fat_erv,
'serving_carb': carb_serv
} )
# User_diet_dict.append = ( {
# 'food_name': item_name,
# 'amount': num_serv*size_serv,
# 'serving_calorie': num_serv*calorie_serv,
# 'serving_protien': protein_serv,
# 'serving_fat': fat_serv,
# 'serving_carb': carb_serv
# } )
# add to the dictonary/list
print food_dict
add_food()
if ask_to_add_another == 'n':
return False
函数的作用是:更新食物字典并添加到列表中
我得到了错误
Traceback (most recent call last):
File "MACROCALC.py", line 156, in <module>
main()
File "MACROCALC.py", line 35, in main
add_food()
File "MACROCALC.py", line 130, in add_food
food_dict.append( {
NameError: global name 'food_dict' is not defined
回溯(最近一次呼叫最后一次):
文件“MACROCALC.py”,第156行,在
main()
文件“MACROCALC.py”,第35行,主
添加食物()
文件“MACROCALC.py”,第130行,添加食物
食物目录附加({
名称错误:未定义全局名称“食品目录”
我觉得这似乎是因为字典不是全球性的
如果有人好奇-->
开放的建议!Noob程序员期待变得更好
感谢您的帮助!在每个子程序中添加以下内容-
global food_dict
将
food\u dict
声明为global
,放在每个将改变列表的函数的顶部。对于列表
来说,改变基本上是赋值,而不是使用append()
。通常,使用类的方法并不构成我上面描述的改变。见下文:
def init():
global food_dict
# assignment is manipulation
food_dict = [{'name': 'apple'}, {'name': 'orange'}]
def next():
# notice no 'global' usage but this still works
food_dict.append({'name': 'kiwi'})
def lastly():
global food_dict
# assign to empty list
food_dict = [{}]
>>> init()
>>> print food_dict
[{'name': 'apple'}, {'name': 'orange'}]
>>> next()
>>> print food_dict
[{'name': 'apple'}, {'name': 'orange'}, {'name': 'kiwi'}]
>>> lastly()
>>> print food_dict
[{}]
这是因为
food\u dict
不在add\u food
的范围内。解决这一问题的一种方法是将其全局化,但更好的方法是将这些print
s更改为return
s,调用food\u数据库
,并将其输出存储在一个变量中。