How to get user to type in a specific word to get a specific response in python

43 views Asked by At

I'm trying to make a simple quiz in python to check if the user is able to determine who my friends are. It is still not complete but basically I want the program to give a certain response if the user enters a certain input that matches my list. For example, the first question is "Who is my BEST friend?". If the user enters "Mariah", I want the program to tell them they are correct and then move on to the next question. My problem is when I enter that, the response should check for the match and it doesn't match up even for the correct input. It completely ignores if my if statement for getting the "you're correct!" statement and jumps directly to the else statement: "sorry you're incorrect". How do i fix this?

list = ["Mariah", "Julian", "Gabbi"]
variable = input("do you know my friends? yes or no? ")
if variable.lower() == "yes":
    print("oh really... ")
    variable2 = input("what is my best friend's name? ")
    if variable2.lower() == list[0]:
    print("you are correct, but she's not my only friend")
    else:
        print("sorry you're WRONG!")
elif variable.lower() == "no":
     print("i thought so...")
else:
    print("that answer didn't make sense")
2

There are 2 answers

0
Scary Wombat On

You are converting the input to lower case but not the list value

try

if variable2.lower() == list[0].lower():
0
fam-woodpecker On

Your indentation is wrong on the code snippet you shared, but I assume that might just be formatting for the question.

Also, try not to use variable names that override keywords or defined functions in python, for example list.

Additionally, you compare variable.lower() to the list. So you are comparing a forced lowercase string, to the name 'Mariah', which has a capital letter.

You can fix this by

friends = ["Mariah", "Julian", "Gabbi"]
variable = input("do you know my friends? yes or no? ")
if variable.lower() == "yes":
    print("oh really... ")
    variable2 = input("what is my best friend's name? ")
    if variable2.lower() == friends[0].lower():
        print("you are correct, but she's not my only friend")
    else:
        print("sorry you're WRONG!")
elif variable.lower() == "no":
    print("i thought so...")
else:
    print("that answer didn't make sense")