I’m trying to build a quick little quiz program. I know I’ve got most of it right but when it comes to the code where I ask the user if they have a pet, I’m having a little bit of an issue.
Still pretty new to Python and could really appreciate the help! This small problem is starting to give me quite a headache.
Also, I’m running Python 3.7.1
yes_pets = ["y", "yes", "yeah", "yup", "yeah"]
no_pets = ["n", "no", "nope", "nah", "non"]
name = input("What is your name?: ").strip().capitalize()
age = int(input("How old are you?: ").strip().lower())
place = input("Where do you live?: ").strip().capitalize()
work = input("What do you do for a living?: ").strip().lower()
hobbies = input("What is your favorite hobby?: ").strip().lower()
music = input("Who is your favorite musician?: ").strip().title()
travel = input("What country do you want to visit the most?: ").strip().capitalize()
pets = input("Do you have any pets? (y/n): ").strip().lower()
if pets in yes_pets:
pets = "your furbaby"
elif pets in no_pets:
pets = "that $$$"
else:
for pets in yes_pets or no_pets:
pets = input("Sorry didn't catch that. Do you have any pets (y/n)?: ").strip().lower()
if pets in yes_pets:
pets = "your furbaby"
elif pets in no_pets:
pets = "that $$$"
print("\nHi {}! You are {} years old and you live in {}.\nYou work as an {} but you do it for {}.\nWhen you're not working we could probably find you\n{} listening to some {}\ndreaming of going to {} someday.".format(name, age, place, work, pets, hobbies, music, travel))
— I want it to —
Reference the users input of the variable PETS
with the acceptable answers in lists yes_pets
and no_pets
. If the input is in the yes_pets
list, I want pets = “your furbaby”
. If the input is in the no_pets
list I want pets = “that $$$”
. However, if the user gives an input that is NOT IN either of those lists, I want it to loop and keep asking that question again until the user gives an acceptable input.
— What I think is happening —
I believe the first if and elif statements work just fine as they work when I give an acceptable input. But when the for loop executes it keeps looping the question even when the user gives an acceptable input. I tried for pets not in yes_pets or no_pets:
but that logic doesn’t seem to work. So leaving the NOT out, what I think is happening is that its infinitely looping because the user gave an answer that makes the condition of the loop true? When the user gives another answer that still isn’t in the list it still loops because of the ELSE?