私は、ユーザーが食べたカロリーの数を数え続け、これをデータベースに挿入しながら、ユーザーが食べたカロリーの量に関するフィードバックを与えたいと考えています。私は関数の外TotalCalories
を定義し、(それは別の関数内で使用することができるように)グローバル変数に設定すると、私はエラーを取得するUnboundLocalError: local variable 'TotalCalories' referenced before assignment
関数の中に積算合計を保持し、それが関数の外でアクセスできるようにするには?
def FoodSelection(choice):
global CurrentPupil
CurrentPupil = ""
global FoodSelectionWindow
FoodSelectionWindow = Toplevel()
FoodSelectionWindow.configure(bg="black")
DateAdded = datetime.date.today()
TimeTaken = 0
NoFoodOption = 0
#The validation timer starts here:
while TimeTaken < 2:
time.sleep(60)
TimeTaken = TimeTaken +1
#and ends here
class food():
def __init__(self, name = "no name", calories = 0, photo = ""):
self.name = name
self.calories = calories
self.photo = photo
BoiledEggs = food("Boiled Egg", 155, PhotoImage(file="BoiledEgg.gif"))
ScrambledEggs = food("Scrambled Egg", 148, PhotoImage(file="ScrambledEgg.gif"))
FriedEggs = food("Fried Egg", 196, PhotoImage(file="FriedEgg.gif"))
PoachedEggs = food ("Poached Egg", 143, PhotoImage(file="PoachedEgg.gif"))
Toast = food("Toast", 313, PhotoImage(file="Toast.gif"))
Bacon = food("Bacon", 514, PhotoImage(file="Bacon.gif"))
Cereal = food("Cereal", 379, PhotoImage(file="Cereal.gif"))
Porridge = food("Porridge", 68, PhotoImage(file="Cereal.gif"))
NoBreakfast = food("No Breakfast", 0, PhotoImage(file="NoBreakfast.gif"))
def NoFood():
FoodChoices("No Food")
def BoiledEggsFunct():
FoodChoices("Boiled Eggs")
def FriedEggsFunct():
FoodChoices("Fried Eggs")
def ScrambledEggsFunct():
FoodChoices("Scrambled Eggs")
def PoachedEggsFunct():
FoodChoices("Poached Eggs")
def ToastFunct():
FoodChoices("Toast")
def BaconFunct():
FoodChoices("Bacon")
def CerealFunct():
FoodChoices("Cereal")
def PorridgeFunct():
FoodChoices("Porridge")
global TotalCalories
TotalCalories = 0
def FoodChoices(selection):
if selection == "No Food":
TotalCalories = NoBreakfast.calories
elif selection == "Boiled Eggs":
TotalCalories = BoiledEggs.calories + TotalCalories
elif selection == "Fried Eggs":
TotalCalories = FriedEggs.calories + TotalCalories
elif selection == "Scrambled Eggs":
TotalCalories = ScrambledEggs.calories + TotalCalories
elif selection == "Poached Eggs":
TotalCalories = PoachedEggs.calories + TotalCalories
elif selection == "Toast":
TotalCalories = Toast.calories + TotalCalories
elif selection == "Bacon":
TotalCalories = Bacon.calories + TotalCalories
elif selection == "Cereal":
TotalCalories = Cereal.calories + TotalCalories
elif selection == "Porridge":
TotalCalories = Porridge.calories + TotalCalories
if choice == 'chris':
CurrentPupil = "Chris"
with db:
cursor.execute(''' INSERT INTO 'Breakfast_History' (CaloriesTotal, DateAddded, PupilNames, PupilID) VALUES (?,?,?,?)''',
([TotalCalories, DateAdded, CurrentPupil,"1"]))
elif choice == 'josh':
CurrentPupil = "Josh"
with db:
cursor.execute(''' INSERT INTO 'Breakfast_History' (CaloriesTotal, DateAddded, PupilNames, PupilID) VALUES (?,?,?,?)''',
([TotalCalories, DateAdded, CurrentPupil,"2"]))
elif choice == 'sam':
CurrentPupil = "Sam"
with db:
cursor.execute(''' INSERT INTO 'Breakfast_History' (CaloriesTotal, DateAddded, PupilNames, PupilID) VALUES (?,?,?,?)''',
([TotalCalories, DateAdded, CurrentPupil,"3"]))
elif choice == 'daniel':
CurrentPupl = "Daniel"
with db:
cursor.execute(''' INSERT INTO 'Breakfast_History' (CaloriesTotal, DateAddded, PupilNames, PupillID) VALUES (?,?,?,?)''',
([TotalCalories, DateAdded, CurrentPupil, "4"]))
elif choice == 'jim':
CurrentPupil = "Jim"
with db:
cursor.execute(''' INSERT INTO 'Breakfast_History' (CaloriesTotal, DateAddded, PupilNames, PupilID) VALUES (?,?,?,?)''',
([TotalCalories, DateAdded, CurrentPupil,"5"]))
elif choice == 'sean':
CurrentPupil = "Sean"
with db:
cursor.execute(''' INSERT INTO 'Breakfast_History' (CaloriesTotal, DateAddded, PupilNames, PupilID) VALUES (?,?,?,?)''',
([TotalCalories, DateAdded, CurrentPupil,"6"]))
db.commit()
def FeedbackScreen():
FinishWindow = Toplevel()
if TotalCalories > 0 and TotalCalories < 1000:
HealthyLabel = Label(FinishWindow, text = "Congratulations, you are healthy!", font=("Comic Sans MS", 25), fg = "light green",
bg = "black")
HealthyLabel.grid(columnspan=10)
elif TotalCalories > 1000:
UnhealthyLabel = Label(FinishWindow, text = "Try to eat healthier tomorrow. See personalised advice", font=("Comic Sans MS", 25), fg = "yellow",
bg = "black")
UnhealthyLabel.grid(columnspan=10)
elif NoFoodOption == 1:
NoFoodLabel = Label(FinishWindow, text = "Not eating can be harmful to your health. See personalised advice", font=("Comic Sans MS", 25), fg = "red",
bg = "black")
NoFoodLabel.grid(columnspan=10)
else:
Error = Label(FinishWindow, text = "error", font=("Comic Sans MS", 25), fg = "red",
bg = "black")
NoFoodLabel.grid(columnspan=10)
あなたが見ることができるように、私はAでTotalCalories
を使用する必要がありますこのコードではさまざまな関数が使用されているので、グローバル変数にすることでこれを実行できると思いました。
提案がありますか?ここで
、このような入れ子構造を持ってする理由はほぼ確実にありません。関数を呼び出す関数に値を代入するだけで、関数の値を '返す '必要があります。 – roganjosh