0

I'm trying to make a loop that detects when a user has eaten enough food. This code makes sense to me but apparently it's wrong. I feel like it has something to do with the conversion from int to str or vice versa but I'm not sure.

food = 0
tries = 0
while food < 10 and tries < 3:

  food = food + int(input("How much food would you like to eat?: "))
  print("You've eaten " + str(food) + " amounts of food.")
  tries += 1
  food = food + int(input("It's not enough food. How much more would you like to eat?: "))
  print("You've eaten " + str(food) + " amounts of food.")
  tries += 1
  food = food + int(input("It's still not enough. You have one more chance to eat. How much more?: "))
  print("You've eaten " + str(food) + " amounts of food.")
  tries += 1
  print("It's too late. You're dead")
else:
  print("You've eaten enough food.")
  • Can you provide a bit more context? i.e. what environment are you running this code in? what language? etc.? – Danoz Mar 22 '20 at 20:49
  • While this code is poorly laid out, for me it does correctly run and the loop ends. – Rashid 'Lee' Ibrahim Mar 22 '20 at 20:51
  • Your code works fine for me – hurlenko Mar 22 '20 at 20:51
  • 1
    If you are talking about why it keeps running after food is greater than 10.. that's because the while part is only evaluated once per iteration of the loop and your code is written to only run one iteration ever because you add 3 to tries in a single iteration – Rashid 'Lee' Ibrahim Mar 22 '20 at 20:54
  • I think you want to look into what `break` statements do ;). The whole body of your while loop will run at least once, so even though the first input is more than 10, it will prompt the user again. And as said before the logic is indeed poorly laid out :) – Clepsyd Mar 22 '20 at 20:56
  • Daniel, that works but how do I do it using a while loop? A while loop runs until the conditions are false. Therefore it should end. Why doesn't it? – alexanderfeazell Mar 22 '20 at 20:56
  • Oh, I get it, Clepsyd. I didn't know a while loop has to completely run at least once. – alexanderfeazell Mar 22 '20 at 20:57
  • "A while loop runs until the conditions are false." Not UNTIL: IF. It runs entirely once, then checks the conditions again – Clepsyd Mar 22 '20 at 20:58
  • Yeah, I didn't know that. This is my third day learning python lol – alexanderfeazell Mar 22 '20 at 20:59
  • I know I could've done this with if statements but I wanted to test my knowledge with loops. Guess I need to learn more about loops. :p – alexanderfeazell Mar 22 '20 at 21:00
  • I didn't even think of using nonnumerical characters – alexanderfeazell Mar 22 '20 at 21:08

3 Answers3

1

You don't have a loop, but one sequence of inputs:

food = 0
food = food + int(input("How much food would you like to eat?: "))
print(f"You've eaten {food} amounts of food.")
if food < 10:
    food = food + int(input("It's not enough food. How much more would you like to eat?: "))
    print(f"You've eaten {food} amounts of food.")
    if food < 10:
        food = food + int(input("It's still not enough. You have one more chance to eat. How much more?: "))
        print(f"You've eaten {food} amounts of food.")
        if food < 10:
            print("It's too late. You're dead")
if food >= 10:
    print("You've eaten enough food.")

You can turn this into a loop over the input texts with a for-loop:

food = 0
for text in [
    "How much food would you like to eat?: ",
    "It's not enough food. How much more would you like to eat?: ",
    "It's still not enough. You have one more chance to eat. How much more?: ",
]:
    food += int(input(text))
    print(f"You've eaten {food} amounts of food.")
    if food >= 10:
        print("You've eaten enough food.")
        break
else:
    print("It's too late. You're dead")
Daniel
  • 42,087
  • 4
  • 55
  • 81
0

The problem occurs when a user enters a non-numerical character. As the int method does not filter the string you need to do so manually. Following the code of @Daniel, I have added filtration to the code. Here is the working code with an actual loop:

food = 0

def filterNonNumChars(input):
  input = str(input)
  allowedChars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  output = ""
  for i in range(0, len(input)):
      if input[i] in allowedChars:
        output += input[i]

  return output

for text in [
    "How much food would you like to eat?: ",
    "It's not enough food. How much more would you like to eat?: ",
    "It's still not enough. You have one more chance to eat. How much more?: ",
]:
    food += int(filterNonNumChars(input(text)))
    print(f"You've eaten {food} amounts of food.")
    if food >= 10:
        print("You've eaten enough food.")
        break
else:
    print("It's too late. You're dead")
Geetansh G
  • 17
  • 7
0

Not sure the logic of your loop. "while ... else..." is not common and clear, and it can be replaced by "while " and "if" which makes logic clear.

food = 0
tries = 0
while food < 10 and tries < 3:
  food = food + int(input("How much food would you like to eat?: "))
  print("You've eaten " + str(food) + " amounts of food.")
  tries += 1
  food = food + int(input("It's not enough food. How much more would you like to eat?: "))
  print("You've eaten " + str(food) + " amounts of food.")
  tries += 1
  food = food + int(input("It's still not enough. You have one more chance to eat. How much more?: "))
  print("You've eaten " + str(food) + " amounts of food.")
  tries += 1
  print("It's too late. You're dead")

if (food >= 10 or tries >= 3):
  print("You've eaten enough food.")

You may refer to here

jsz jsz
  • 16
  • 3