My IF statement is not working as it should, how do I fix this?

Centr1fuge

I am creating a quiz program that draws a question and answer from a .txt file. The questions and answers are in the format:

question,answer

in the .txt file. The program picks one at random, splits it at the comma and asks the question. It then matches the users answer to the questions answer and checks if it is correct like this:

if userAnswer == question[1]:
    print('Correct!')
else:
    print('Incorrect, better luck next time!')

Every time I run the program I get no errors but no matter what I put, the program tells me I'm incorrect. I've asked a few friends but they are all as confused as me and none of us have any clue what the problem is. The code the splits the question and answer:

f = open('Music.txt', 'r')
question = random.choice(list(f))
question = question.split(',')
userAnswer = input(f'Who sang {question[0]}? ').lower()

I should clarify, the .txt file questions are all songs and all the answers are bands/singers. None of the answers contain comma's.

Gabip

I guess that since you questions,answers are new line separated, the answer from you read from the file ends with '\n'. In order to remove it, run: answer = answer.rstrip() and then compare it to the answer provided as the user input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fix my Haskell conditional statement?

How do I fix my conditional statement for marks input in Python?

How do I fix my code about a script that stopped working?

How do I fix my ctx not working (Discord Bot in Python)?

How do I fix my UI Buttons not working in Unity?

How do i fix this error? Switch statement

How do I fix my while statement in order to only take numbers from an input?

My PHP code is working but "printf or echo" statement is not! How to fix it?

Why is this code not working, How do i fix it?

My code for this newton's cradle isn't working. How do I fix this?

My python installer freezes while working. How do I fix it?

My trackpad stopped working (not detected) after update, how do I fix this?

How do I fix my neomuttrc syntax?

How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

How do I fix my FirebaseRecyclerAdapter on a fragment?

How do I fix plotting my boxplot?

How should I fix this

jinja if statement is not working. I don't know how to fix it

I was trying to create randomly positioned objects that should not collide. My collider does not seem to detect properly. How do I fix it?

How should I fix my code for Sock Merchant with runtime error

How should I fix my for loop code for summing a range?

What version of Cordova do I have and how should I fix it?

How do I call this attribute to my if statement?

float right is not working in my case , what should i do?

how should i put my method(with array) in a switch statement in java?

How can i fix segmentation fault in my code and what is the process I should follow to fix this in the future?

Variables that should be defined are giving me NameErrors, how do I fix it?

How do I fix my logo from overlapping my menu?

how do i fix my problem with my tuple in python

TOP Ranking

HotTag

Archive