Thursday 15 August 2013

python - Same list/dictionary in multiple functions -


I'm new to Python and I'm still learning to deal with lists and dictionaries at present.

I have these two functions

  def food_database (item_name, size_serv, calorie_serv, protein_serv, carb_serv, fat_serv): # When users are asked for this, the list is used in # 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): # how to use to show the diet plan for user User_diet_dict = [{ 'food_name': ITEM_NAME, 'balance': num_serv * size_serv, 'serving_calorie': num_serv * calorie_serv, 'serving_protien': protein_serv, 'serving_fat': fat_serv, 'serving_carb': carb_serv}] prints User_diet_dict   

I have this other work

  def add_food (): ask_to_add_an_other_otherother = raw_input ("Would you like to add other food? (Y / n) ") if ask_to_add_another == 'y': # Update item_name = Raw_input (" What is the name of the food you want to add?) "Size" = input ("size in every service of% s (gm ) what is? "% Item_name) Kailori_srw = input (" how many calories% s of each service? "% Item_name) Protin_srw = input carb_serv = ("% protein many grams in each serving of S? "% Item_name) input ("How many grams of carbohydrate are in every service of% s?"% Item_name) fat_serv = input ("How many grams of fat What's the number of servings of% s you want to add? Food_dict.append ({'food_name': 'item_name', 'serving_size': size_serv '', 'item_name' serving_calorie ': protein_serv,' serving_fat ': calorie_serv,' serving_protien '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 #}) # Dictonary / catalog print food_dict to add_food) If adding ask_to_add_ano (the == 'n': Return the wrong   

add_food () function updates the food_dict dictionary and add it to the list Is obtained.

I get an error

  traceback (most recent call final): file "MACROCALC.py", line 156, & lt; Module & gt; The main () file "MACROCALC.py", line 35, in the main add_food () file "MACROCALC.py", line 130, add_food at food_dict.append ({NameError: global name is not defined 'food_dict'  < / Pre> 

I feel as if it is not going to be global, it is happening.

If someone is curious then my code is ->

Open

Annotate for suggestions food_dict like < Code> global as the list needs to be changed. Chi is basically assigned for mutate, is not the use of an attachment () Generally, using the methods of class does not mean mutation that I See below:

  def init (): global food_dict # assignment is manipulation Food_dict = {{'name': 'apple'}, {'name': 'orange'}] Def next (): # Notices 'global' usage but it still works food_dict.append ({'name': 'k Iwi'}) DEF end (): global food_space # b Assigned to Li list food_dict = [{}]> gt; & Gt; & Gt; Init ()> & gt; & Gt; & Gt; Print food_dict [{'name': 'apple'}, {'name': 'orange'}]> gt; & Gt; & Gt; Next ()> & Gt; & Gt; Print food_dict [{'name': 'apple'}, {'name': 'orange'}, {'name': 'kiwi'}]> gt; & Gt; & Gt; Finally () & gt; & Gt; & Gt; Print food_dict [{}}    

No comments:

Post a Comment