-4

I am having trouble with this trivia program. I have to be able to read the questions and answers from a text file using a while loop. I then have to ask the user what the answer is and tell them if they are correct or not. Then I have to tell them how many answers they got correct. Last, I have to ask them if they wish to run it again.

My problem is when I run the while loop it runs past the 20th question in the text file. My code is below, if you could help me that would be great.

print('Welcome to The Computer Trivia Program!')
F=open('Culminating Project Trivia Questions.txt','r')

AC=0
while F!='':
    Q=F.readline()
    print(Q)
    C1=F.readline()
    print(C1)
    C2=F.readline()
    print(C2)
    C3=F.readline()
    print(C3)
    C4=F.readline()
    print(C4)
    A=input('Enter an Anwser:')
    ANS=F.readline()
    print(ANS)

    if A==ANS[14]:
        print('Correct')            
        AC=AC+1        
    elif A!=ANS[14]:            
        print('Incorrect')            
        AC=AC        
    if F=='':            
        break

F.close()

print('Your Total Amount of Correct Answers out of 20 is:',AC)
print('Thankyou for Playing Computer Trivia!')
cs95
  • 379,657
  • 97
  • 704
  • 746
Nada
  • 3
  • 3
  • Your code is not properly indented. That way, it won't run as expected. – Alfabravo Jun 13 '17 at 21:07
  • Hi yes I do know that but I wasn't sure how to indent when posting it. My code in python does have indents. – Nada Jun 13 '17 at 21:08
  • If you copy and paste it, it should keep indentation... – Alfabravo Jun 13 '17 at 21:08
  • @Nada why are you reading multiple lines at once? – cs95 Jun 13 '17 at 21:09
  • is there a reason for when I run it, it keeps asking me to enter an answer even after the whole file has been read – Nada Jun 13 '17 at 21:10
  • 1
    @Nada Yes, because `F` is a file pointer, and will _never_ be equal to `''`. – cs95 Jun 13 '17 at 21:10
  • in my text file I have a question, then 4 choices after, then it asks the user to input the answer, it then gives the correct answer. – Nada Jun 13 '17 at 21:11
  • would you have any suggestions on what i can set the while loop, or do I need add extra code? – Nada Jun 13 '17 at 21:15
  • thankyou so much for your help Coldspeed – Nada Jun 13 '17 at 21:34
  • @Nada No problems. Do consider marking my answer and closing the question. Much appreciated. – cs95 Jun 13 '17 at 21:35
  • 1
    @L3viathan We generally _don't_ make edits to fix indentation on Python questions since that can accidentally correct the OP's problem. Instead, we try to get the OP to fix it themself. OTOH, if the OP thinks your edit has changed their code I guess they can always modify it or revert it. ;) – PM 2Ring Jun 13 '17 at 21:42
  • @PM2Ring In this case, the indentation wasn't the problem, though, it was the comparison of a string with the file pointer. If there's supposed to be _any_ value in this question to future readers, I think it should be correctly indented (as the OP claims it was, locally, anyways). – L3viathan Jun 13 '17 at 21:44
  • 1
    @L3viathan I certainly agree that the question code needs to be indented, but it should be indented the way it is on the OP's machine, not how _we_ think it ought to be indented. We can _assume_ the `if...elif` stuff was indented correctly in the OP's real code, but we don't know that for sure without further evidence from the OP. – PM 2Ring Jun 13 '17 at 21:49
  • Hi, could anyone possibly help me figure something else out. Within the while loop, after the 20 questions, I need to say what amount they got correct, right after the 20. Then I have to ask the user if the would like to run the code then. I know it has to be in the while loop, if you could help that would be great. – Nada Jun 13 '17 at 22:37
  • You can do that by inserting `while True:` on the 2nd line, before the `F=open(` line, indenting _everything_ under that (except for the very last line) by 4 more spaces , and after the `print('Your Total Amount` (but still inside the new `while` loop) use an `input` call to ask if they want to have another go. If they say no, use `break` to get out of the loop. You should take a look at Kevin's [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/4014959), there are some great ideas there. – PM 2Ring Jun 13 '17 at 23:25
  • Thankyou so much for your help PM 2Ring. – Nada Jun 14 '17 at 00:42

1 Answers1

1

Here's your code indented and fixed, and did a little housekeeping.

Do not perform string comparisons with file pointers. Was happening in 2 places. You have to call F.readline() and figure out if that is ''.

You don't need to specify elif with a condition - else is enough.

Furthermore, some of those readline and print statements can be collapsed, but I'll leave them be.

print('Welcome to The Computer Trivia Program!')
F = open('Culminating Project Trivia Questions.txt', 'r')
AC = 0
t = F.readline()
while t:
    Q = t
    print(Q)
    C1=F.readline()
    print(C1)
    C2=F.readline()
    print(C2)
    C3=F.readline()
    print(C3)
    C4=F.readline()
    print(C4)
    A=input('Enter an Anwser:')
    ANS=F.readline()
    print(ANS)

    if A == ANS[14]: 
        print('Correct')
        AC = AC + 1

    else: 
        print('Incorrect')

    t = F.readline()

F.close()
print('Your Total Amount of Correct Answers out of 20 is:', AC)
print('Thankyou for Playing Computer Trivia!')
cs95
  • 379,657
  • 97
  • 704
  • 746