Python 如何使用while循环添加值?

Python 如何使用while循环添加值?,python,python-3.x,Python,Python 3.x,我正在编写一个简单的python程序,帮助用户跟踪他们消耗的卡路里数量,并显示用户是否超过或在他们一天的卡路里目标之内 这是到目前为止我的代码 caloriesGoal = float(input('Enter the calorie goal for number of calories for today: ')) numberOfCaloriesConsumed = float(input('Enter the number of calories consumed today')) t

我正在编写一个简单的python程序,帮助用户跟踪他们消耗的卡路里数量,并显示用户是否超过或在他们一天的卡路里目标之内

这是到目前为止我的代码

caloriesGoal = float(input('Enter the calorie goal for number of calories for today: '))

numberOfCaloriesConsumed = float(input('Enter the number of calories consumed today'))
totalConsumed = 0
while (numberOfCaloriesConsumed > 1500 and numberOfCaloriesConsumed < 3000):
      numberOfCaloriesConsumed = float(input('How many calories did you consumed?'))
      totalConsumed += numberOfCaloriesConsumed
      count = count + 1
print('Your total calories consumed is: ' , totalConsumed)

if(caloriesGoal > totalConsumed):
      print('The user is within their goal by ', (caloriesGoal - totalConsumed))
else:
      print('The user exceeded their goal by', (totalConsumed - caloriesGoal))
caloriesGoal=float(输入('输入今天卡路里数量的卡路里目标:'))
numberOfCaloriesConsumed=float(输入(“输入今天消耗的卡路里数量”))
总消耗=0
而(消耗的卡路里数>1500,消耗的卡路里数<3000):
numberOfCaloriesConsumed=float(输入('您消耗了多少卡路里?'))
总消耗量+=消耗的卡路里数
计数=计数+1
打印('您的总卡路里消耗量为:',总消耗量)
如果(卡路里目标>消耗总量):
打印('用户在其目标范围内',(卡路里目标-总消耗量))
其他:
打印('用户超出目标',(总消耗量-卡路里目标))

试试看,输出结果会更清楚地显示您希望输入的内容

while True:
    caloriesGoal = float(input('Enter the calorie goal the day: '))
    if 1500 <= caloriesGoal <= 3000:
        break
    else:
        print("Error: Goal must be between 1500 and 3000 calories!")

totalConsumed = 0
items = 0

while True:
      caloriesConsumed = float(input('Item {}: How many calories was it? (-1 to stop)'.format(items+1)))

      if caloriesConsumed < 0:
          print("Input stopped")
          break

      totalConsumed += caloriesConsumed 
      items += 1

      if totalConsumed > caloriesGoal:
          print("Goal reached/exceeded!")
          break

print('You consumed {} calories in {} items.'.format(totalConsumed, items))

if caloriesGoal > totalConsumed:
      print('The user is within their goal by ', (caloriesGoal - totalConsumed))
else:
      print('The user exceeded their goal by', (totalConsumed - caloriesGoal))
为True时:
卡路里目标=浮动(输入('输入当天的卡路里目标:'))
如果消耗了1500欧元:
打印('用户在其目标范围内',(卡路里目标-总消耗量))
其他:
打印('用户超出目标',(总消耗量-卡路里目标))

欢迎使用Python!这是我的第二语言,但我喜欢它,就像它是我的第一语言一样。你的代码中很少有奇怪的事情发生

有趣的是,你正在检查消耗的卡路里数量。如果你吃的是Krispy Kreme甜甜圈,你会摄入190卡路里。让我们回顾一下您的while:

while(numberOfCaloriesConsumed>1500和numberOfCaloriesConsumed<3000):

如果我们看看这条线

numberOfCaloriesConsumed=float(输入(“输入今天消耗的卡路里数量”)

。。。我们为Krispy Kreme甜甜圈输入190,那么我们就不会进入循环。您可能希望将while更改为具有中断机制的无限循环:

while 1:
    numberOfCaloriesConsumed = input('How many calories did you consumed?')
    if (numberOfCaloriesConsumed == "q") or (totalConsumed > 3000): # keep numberOfCaloriesConsumed as a string, to compare for the break
        if (totalConsumed < 1500):
            print("You need to eat more!\n")
        else:
            break
    else:
        if (totalConsumed + int(numberOfCaloriesConsumed)) > 3000:
            print("You eat too much! You're done now!\n")
            break
        totalConsumed = totalConsumed + int(numberOfCaloriesConsumed) #make the string an int, if we're adding it
而1:
numberOfCaloriesConsumed=输入(“您消耗了多少卡路里?”)
如果(numberOfCaloriesConsumed==“q”)或(totalConsumed>3000):#将numberOfCaloriesConsumed保留为一个字符串,以便在中断时进行比较
如果(总消耗量<1500):
打印(“您需要多吃!\n”)
其他:
打破
其他:
如果(总消耗量+int(消耗热量的数量))>3000:
打印(“你吃得太多了!你现在吃完了!\n”)
打破
totalConsumed=totalConsumed+int(numberOfCaloriesConsumed)#如果要添加字符串,请将其设置为int
这样,当用户手动退出循环时(或者如果他们输入了太多卡路里),代码循环就会退出。现在,您的用户应该能够输入卡路里数据并将其相加

另外,我会删除计数器,因为此循环不需要计数器。我们有几个不需要柜台的理由:

  • 计数器不会直接影响循环(例如,计数器<5时无
  • 我们的计算机要求用户输入,因此程序停止并将控制权交给人类,因此没有无限循环
  • 我们使用break语句将我们从(新)循环中移除

@pp_u刚刚添加了一些缩进您将一个“计数器”变量初始化为零,然后递增另一个名为“计数”的变量。他们应该有相同的名字。此外,缩进使您很难理解代码。如果您修复了计数器变量和缩进,它看起来应该可以工作。@JosephJames我刚刚更新了我的代码。但是还没有进展@cricket我把计数器改为countIt,它仍然没有被使用。你为什么需要它?消耗的热量不应该在更低的范围内吗
caloriesGoal
应该在1500-3000范围内,而不是每个项目。问题中的代码有严重的逻辑缺陷。@zyxcom要正确标记人员,请不要在@和名称之间留空格。正如我所说,这是您原始代码中的逻辑,请更改您认为的方式fit@cricket我就这么做了。非常感谢!!现在看起来好多了,OP似乎很满意。但是第一个循环实际上可能只是一个
if