Day 015 - Project 15 : All About the Loop

Hey @Inimay welcome to the forums!

Try using this:

print("Fill in the blank lyrics!")
print()
answer = ""
while True:
    print("Oh, I wish I had a _____ so long")
    answer = input("")
    if answer == "river":
        print("Correct answer!")
        break
    else:
        print("Wrong answer! Try again.")

It will see if it is correct if it is it will break out of the loop, if it doesn’t it, it has to be incorrect.

4 Likes

The loop will end, but because each iteration of the while loop must finish even if the while loop’s expression is no longer true, your “Wrong answer! Try again!” is displayed.

I don’t even know what I was talking about in my last post xD

4 Likes

Thank you very much, @SalladShooter!

Thanks a lot, @bobastley!
The Day 15 tutorial of the 100 Days of Python Challenge has the following example for a while loop with text:

exit = ""
while exit != "yes":
  print("🥳")
  exit = input("Exit?: ")

This runs just fine in the video (i.e. the loop ends without an extra " :partying_face:" emoji when the expression is no longer true).

So I don’t know what I’m getting wrong here. :sweat:

1 Like

just a tip: you don’t need to use 2 prints at the start. Use \n to represent a new line

print("A new line will follow\n")

and, using \n, you can eliminate the need for print on line 5:

  answer = input("Oh,...\n")
4 Likes

Because your input() statement is at the end of the loop, there isn’t any code to run after it, meaning it will check the condition (exit != "yes") because that iteration ended. Because you typed yes into your program, the loop will not run again because that condition evaluates to False.

(Your print() statement is at the start of the loop, so it won’t be able to run again without another iteration)

Comparing it to your original program, a print() statement was placed after the input(), meaning that iteration had not yet ended, so the condition hadn’t checked yet.

2 Likes

Oh, of course! Thanks so much! :hugs:

1 Like

I’m trying to figure out why the code never actually executes. I know the example says to use != instead of ==. But I’m trying to understand what is happening to have it not work.

exit = ""
while exit == 'yes':
  animal = input('What animal sound would you like to hear? ')
  if animal == 'cat':
    print('I hate you')
  elif animal == 'dog':
    print('WOOOOOOOOOOOOOF')
  else:
    print('That is not a real animal. Try again')
  exit = input('Exit? ')

Hi @RobertThomas14 , welcome to the forums!
This is because exit is nothing, so the code will not execute as the loop checks if exit is yes. It isn’t, so the loop won’t execute.
Hope this helps!

So what is happening is this:

  1. Exit is set to nothing.
  2. It checks to see if exit is yes.
  3. It isn’t so it never continues.

But when we change to != 'yes:

  1. Exit is set to nothing
  2. It checks to see if it is equal to anything that isn’t yes.
  3. nothing doesn’t equal yes and thus the while loop executes.
  4. the exit variable is changed locally when the user inputs
  5. the while loop then checks to see if that doesn’t match yes.
  6. If true it will continue the loop. If false it will stop the loop.
  7. Should there be more code below, any reference to exit would then revert back to the global variable from the beginning, which would be nothing

Do I have that right?

Yes!
@RobertThomas14 You got it :partying_face:!

2 Likes

When imputing an animal from my array it says the same thing that it would if the animal; wasn’t in the array, also my exit while loop is not working. Can I get some help?

The input function returns a string, but your animal variable is a list. A list will never == a string. Instead, you should use ChooseAnimal in animal.

You also have your exit thing the wrong way around.

Here is your fixed code:

exit = "no"
while exit == "no":
    animal = ["cow", "dog", "cat", "sheep", "pig", "horse", "donkey", "goat", "chicken", "goose", "duck"]
    chooseAnimal = input("What animal do you want?")
    if chooseAnimal in animal:
        print("Yes that is an animal!")
    else:
        print("no that is not an animal")
    exit = input("Exit?:")
1 Like

This is what i’ve searched. Just add another variable :-))