def calorie():
protein = 0
fat = 0
carb = 0
tlist = [protein, fat, carb]
for i in tlist:
print('Enter food ' + str(i))
i = input()
print(protein)
print(carb)
print(fat)
calorie()
right now it prints 'enter food 0' I want it to loop and ask each time for protein fat and carb and then save it so i can put it into a database later. The code i have right now is 4 different def that points to protein then carb and so on however after thinking about it i thought it would be easier to just loop it in 1 single def however i cant get it to work. Thank you very much
It prints enter food 0
because by writing for i in tlist
you are not iterating over the names of those variables but instead their value. protein
, fat
, and carb
each have a value of 0, which is what Python prints.
Try using a dictionary?
calorie = {'protein': 0, 'fat': 0, 'carb': 0}
for key in calorie:
value = input(f"Enter value of {key}: ")
calorie[key] = int(value)
do you wan't tlist = ['protein', 'fat', 'carb'] this thing as a loop and you will enter how much amount specific things present in food.
def calorie():
protein = 0
fat = 0
carb = 0
u=input("enter food name") #here you type food which detailed you wana insert.
tlist = ['protein', 'fat', 'carb']
for i in tlist:
s=input("enter {} value in gm".format(i))
print(f" {u} has {s} gm of {i}") #here it print amount nutrient the food has
i hope it will resolve your problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.