1

I am trying to make a quiz using python which reads questions from a text file. I have a variable called ans which is supposed to be the answer which it reads from the file I print the variable and it says what its supposed to say but if I actually type it in it will say its wrong. This is my python code:

right = 0
wrong = 0
num = 0
quest = 0
history = open("history.txt", "r")
lines = history.readlines()
while quest != 3:
    quest = quest+1
    num = num+1
    print("Question", quest)
    question = lines[num]
    print(question)
    num = num + 1
    ans = lines[num]
    print(ans)
    answer = input()
    answer = answer.lower()
    if answer == ans:
        print("correct")
        right = right+1
    else:
        print("Wrong")
        wrong = wrong+0
print("done")

And my history.txt file is formatted like this

Blank Line Blank Line
What is the capital of England?
london
What is 1+1?
2

Thank you.

2
  • 1
    print(repr(ans),"=?=",repr(answer)) im sure you will quickly see your issue ... Commented Oct 27, 2017 at 21:39
  • You need to remove the newline from the user input (or the lines in the file). In many languages that's done with chomp. I don't know how to do it in Python, or whether .readlines or input() do it for you, but that's probably the problem. You can easily try by printing both strings including enclosing characters to see if the final character ends up in a new line. Commented Oct 27, 2017 at 21:41

5 Answers 5

3

The strings returned by history.readlines() have newlines at the end of them, but the string returned by input() doesn't. Use rstrip() to remove any trailing whitespace from the string.

ans = lines[num].rstrip()
Sign up to request clarification or add additional context in comments.

Comments

0

readlines() leaves a newline (\n) character when it splits your string. Try setting ans with

ans = lines[num].rstrip()

Comments

0

Try stepping through your program. If you print out the contents of lines after reading from the file, you'll see it has new line chars:

>>> history = open("history.txt", "r")
>>> lines = history.readlines()
>>> lines
['Blank Line Blank Line\n', 'What is the capital of England?\n', 'london\n', 'What is 1+1?\n', '2']

You'll need to trim the newline character ans = ans.rstrip("\n")

Comments

0

Im pretty sure there is a "new line char" - a.k.a '\n' - when you read from the file. Take a look at this:

a = "123"
b = "123\n"
print(a == b)
Output: False

But:

a = "123"
b = "123\n"
print(a == b.rstrip())
True

Comments

-1

Due to the allready mentioned fact the a \n remains at the end of each line, I recommend using re.split('\n',yourFile) for splitting a string to its lines. The newline character often is not useful in such cases.

3 Comments

This is more of a comment than an answer.
Well, I think it's a useful alternative solution to the given problem... and I think the better one, nevermind
It likely won't work if the file contains carriage returns as well as newline characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.