2

I'm writing a simple python program that helps users keep track of the number of calories that they consume and displaying whether the user is over or within their calorie goal for the day.

Here's my code so far.

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))
8
  • @pp_ just added some indentation Commented Feb 11, 2016 at 5:24
  • 1
    You initialize a 'counter' variable to zero, then you increment a different variable called 'count'. They should both have the same name. Also your indentation makes it hard to figure out your code. If you fix the counter variable and your indentation it looks like it should work. Commented Feb 11, 2016 at 5:25
  • @JosephJames I just updated my code. But no progress yet Commented Feb 11, 2016 at 5:32
  • @ cricket I changed counter to count Commented Feb 11, 2016 at 5:34
  • 1
    It still isn't used. Why do you need it? Commented Feb 11, 2016 at 5:40

2 Answers 2

1

Try this out, the output is much clearer for what you are expected to enter.

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))
Sign up to request clarification or add additional context in comments.

6 Comments

Isn't caloriesConsumed supposed to be in a much lower range? caloriesGoal should be in the range 1500 - 3000, not every item.
The code in the question has serious logical flaws.
@zyxcom To tag people correctly, don't put a space between the @ and the name. And as I said, this is the logic in your original code, change it how you deem fit
@cricket I just did. Thanks so much!!
Looks much better now and the OP seems to be satisfied. But the first loop really could be just an if.
|
0

Welcome to Python! It was my second language, but I love it like it was my first. There are few things going on in your code that's a little strange.

Something interesting is that you're doing a check on numberOfCaloriesConsumed. If you're entering a Krispy Kreme donut, you're going to enter 190 calories. Let's review your while:

while (numberOfCaloriesConsumed > 1500 and numberOfCaloriesConsumed < 3000):

If we take a look at this line ...

numberOfCaloriesConsumed = float(input('Enter the number of calories consumed today'))

... and we enter 190 for our Krispy Kreme donut, then we won't enter the loop. What you may want to change the while to is an infinite loop with a break mechanism:

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

This way, the code loop is exited when your user manually exits the loop (or if they've entered too many calories). Now, your user should be able to input caloric data and have it added up for them.

Also, I would remove the counter because the counter isn't necessary for this loop. We've got a few reasons why we don't need the counter:

  • The counter isn't directly affecting the loop (e.g. no while counter < 5)
  • We have the computer asking for user input, so the program stops and gives control to the human thus no infinite loop
  • We have break statements to remove us from the (new) loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.