【发布时间】:2022-10-13 15:01:54
【问题描述】:
该程序的目标是要求用户输入给定膳食中的卡路里数量。稍后我想添加数字,以便程序记住以前的输入。这只是程序的一部分,但稍后它会返回到 user_input_calories,这样用户就可以根据需要多次输入卡路里。可能 count_calories 不应该为零。有人可以帮我解决这个问题或添加一些我可以查看的参考吗?
start=input('Type add to add a meal:')
while start=='add' or start=='Add':
user_input_calories=input('Enter the number of calories in the meal:')
try:
nr1=int(user_input_calories)
count_calories=0
count_calories=count_calories+nr1
except:
print('You have finised eating for the day')
continue
【问题讨论】:
-
您需要一个循环外的求和变量(即移动 count_calories)。每次循环运行时,循环内定义的任何变量都将被“重置”。
标签: python while-loop count