Brad
Brad

Reputation: 1

If-statements Not Regonizing User Input

I'm new to Python and coding in general. I'm trying to create a fill-in-the-blank quiz to practice. The first part asks the user to choose a difficulty. Once entered, the code appends an empty list. I'll use the list to load questions and answers. However, the issue I'm running into is the if-statements won't recognize user input. I want to make sure the user only inputs easy, medium, or hard. To see if the code worked, I used a print function. However, my code goes right to the else-statement and prints 'Try again.' Any ideas?

Here is my code.

choice = []

new_choice = raw_input('Select your quiz difficulty: Easy, Medium, Hard: ')
choice.append(new_choice)
print choice

if choice == 'easy':
    print 'True'
elif choice == 'medium':
    print 'True'
elif choice == 'hard':
    print 'True'
else:
    print 'Try again.'

Upvotes: 0

Views: 1350

Answers (3)

Larry
Larry

Reputation: 1322

You are comparing lists to variables.

Maybe use:

variable = choice[0]
if variable == 'easy':
    # rest of program

Also, make sure that 'easy', 'medium', and 'hard' have no capital letters.

You can use:

new_choice.lower() # converts to lowercase

before appending to your list

Upvotes: 0

Antimony
Antimony

Reputation: 2240

In its current form choice is a list, while you are trying to compare it to strings/text. Instead try this:

choices = []

new_choice = raw_input('Select your quiz difficulty: Easy, Medium, Hard: ')
choices.append(new_choice)
print choices

if choices[-1] == 'easy':
    print 'True'
elif choices[-1] == 'medium':
    print 'True'
elif choices[-1] == 'hard':
    print 'True'
else:
    print 'Try again.'

I renamed your list to choices to indicate that it holds multiple objects. -1 let's you access the last thing you added to that list. So if you plan to store all the user responses to the list choices, you can perform a comparison against the last answer and proceed.

Upvotes: 2

Kaleb Barrett
Kaleb Barrett

Reputation: 1594

choice is a list containing your input, not the input. The user input you store into the new_choice variable. Try this

choice = raw_input('Select your quiz difficulty: Easy, Medium, Hard: ')
print choice

if choice == 'easy':
    print 'True'
elif choice == 'medium':
    print 'True'
elif choice == 'hard':
    print 'True'
else:
    print 'Try again.'

Upvotes: 1

Related Questions