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.
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
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.
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!
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?:")