Humphrey Brogart
Humphrey Brogart

Reputation: 13

'if a == b[0]' not working when using readlines() SOLVED

I'm writing a Who Wants to Be a Millionare program, but the issue I'm having is that even if the user enters the correct answer, it says it's the wrong answer. I'm new to the whole reading from files thing, so there's probably something I'm missing.

Example output:

    What do you think? 3
    That is the wrong answer!
    The right answer was 3

    Thanks for playing!

Some of the code below:

    questions_file = open('questions.txt', 'r')
questions = questions_file.readlines()
choices_file = open('choices.txt', 'r')
choice = choices_file.readlines()
answers_file = open('answers.txt', 'r')
answers = answers_file.readlines()
prize_file = open('prize.txt', 'r')
prize = prize_file.readlines()

print('Question 1 for',prize[0])
print(questions[0])
print('1 -',choice[0])
print('2 -',choice[1])
print('3 -',choice[2])
print('4 -',choice[3])
answ_1 = int(input('What do you think? '))

if answ_1 == answers[0]:
    print('That is the correct answer!')
else:
    print('That is the wrong answer!')
    print('The right answer was',answers[0])
    print('Thanks for playing!')
    exit()

Upvotes: 1

Views: 57

Answers (1)

Exelian
Exelian

Reputation: 5888

You make answ_1 an int (int(input('What do you think? '))) but the inputs from the files are string.

Either make answ_1 simply a string: answ_1 = input('What do you think? ') or make the answer an int as well: if answ_1 == int(answers[0]):

Upvotes: 3

Related Questions