gpt4 book ai didi

python - 多个函数中的相同列表/字典

转载 作者:行者123 更新时间:2023-12-01 05:42:14 30 4
gpt4 key购买 nike

我是 python 新手,目前仍在学习如何处理列表和字典。

我有这两个功能

    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

add_food() 函数更新 food_dict 字典并添加到列表中。

我收到错误

    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

我觉得好像是因为字典不是全局的,所以才会发生这种情况。

如果有人好奇的话,这是我的代码 --> http://pastebin.com/mc8S6fkS

接受建议!菜鸟程序员希望变得更好!

感谢您的帮助!

最佳答案

在将改变该列表的每个函数的顶部将 food_dict 声明为 global。对于 list 来说,mutate 基本上是分配,而不是使用 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
[{}]

关于python - 多个函数中的相同列表/字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246612/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com