1

I am attempting to ask a question, have the user input the answer. The program has to check the user's answer against the correct answer than keep score. I am having difficulties formatting the while and if statements and reading the lines from the text file.

Quiz Game

def main():

    print("\t\tWelcome to Bible Champion!\n "
        "It can be difficult to remember the characters of the bible.\n "
          "Bible Champion makes it fun and easy with trivia "
          "style questions!\n")
        
    name =(input("Please enter your first name to get started: "))

    print()
                          
    name1 = name[1:] + name[0] + "ay" + "7"
    
    print("Here is player one's game name:",name1,"\n")


  
    qf= open("quiz_game.txt", "r")
    for i in range(2):
        print (qf.readline())

    user_ans = str(input())

    while user_ans[0] =="a" or user_ans[0] == "a.":
            print ("Correct")
            break
    if user_ans[0] =="b" or user_ans[0] == "b.":
            print ("Incorrect")
    if user_ans[0] =="c" or user_ans[0] == "c.":
            print ("Incorrect")
    if user_ans[0] =="d" or user_ans[0] == "d.":
        
            print ("InCorrect")


    qf= open("quiz_game.txt", "r")
    for i in range(4):
            print (qf.readline())

    user_ans = str(input())

    while user_ans[0] =="a" or user_ans[0] == "a.":
                print ("Correct")
                break
    if user_ans[0] =="b" or user_ans[0] == "b.":
                print ("Incorrect")
    if user_ans[0] =="c" or user_ans[0] == "c.":
                print ("Incorrect")
    if user_ans[0] =="d" or user_ans[0] == "d.":
            
                print ("InCorrect")

    qf.close()


main()

Here is what I have so far.

Here is the update output. How do i make it score the first answer and continue to the next set of questions and answers?

    ==========================================================
        ==   Welcome to Bible Champion!   ==
 ==   It can be difficult to remember the characters of the bible.   ==
 ==     Bible Champion makes it fun and easy with trivia style questions!     ==

     ===========================================================
Please enter your first name to get started: kok

Here is player one's game name: okkay7 

(1)Which bible character had strength in his hair?  

a. Samuel b. Peter c.Samson d.John

a
Incorrect
(1)Which bible character had strength in his hair?  

a. Samuel b. Peter c.Samson d.John

c

(2)King David danced out of his ______?     

Updated?

How do I read for instance only line 3 and 4 from the text into python?

I tried using this:

print qf.readline()[0:2]

but it gave me an error.

Community
  • 1
  • 1
  • This is not an answer to the question, but there are much simpler ways to make the checks you are making. Have a look at [this example](https://gist.github.com/daviewales/cdda33dbec5548af72720deb7f1b08b9). – daviewales Mar 08 '18 at 06:13

1 Answers1

0

If I understand your code correctly, you don't need to use while. All you need is if..elif..else

Try this:

print("\t\tWelcome to Bible Champion!\n "
    "It can be difficult to remember the characters of the bible.\n "
      "Bible Champion makes it fun and easy with trivia "
      "style questions!\n")

name =(input("Please enter your first name to get started: "))

print()

name1 = name[1:] + name[0] + "ay" + "7"

print("Here is player one's game name:",name1,"\n")



qf= open("/Users/eon/Downloads/linex.txt", "r")
for i in range(2):
    question = qf.readline()
    answer = False
    while answer == False:
        print (question)
        user_ans = str(input())

        if user_ans[0] =="a" or user_ans[0] == "a.":
            answer = True
            # increment score here
            print ("Correct")
        elif user_ans[0] =="b" or user_ans[0] == "b.":
            print ("Incorrect")
        elif user_ans[0] =="c" or user_ans[0] == "c.":
            print ("Incorrect")
        elif user_ans[0] =="d" or user_ans[0] == "d.":
            print ("InCorrect")


qf= open("/Users/eon/Downloads/linex.txt", "r")
for i in range(4):    
    question = qf.readline()
    answer = False
    while answer == False:
        print (question)
        user_ans = str(input())

        if user_ans[0] =="a" or user_ans[0] == "a.":
            answer = True
            # increment score here
            print ("Correct")
        elif user_ans[0] =="b" or user_ans[0] == "b.":
            print ("Incorrect")
        elif user_ans[0] =="c" or user_ans[0] == "c.":
            print ("Incorrect")
        elif user_ans[0] =="d" or user_ans[0] == "d.":
            print ("InCorrect")

qf.close()
hungersoft
  • 531
  • 4
  • 8
  • I am having an issue with the out put.. I saw that I can use continue to resume the iteration from the file with the next set of readlines but i dont understand how to format it. – Korene Stuart Mar 08 '18 at 04:06
  • Please enter your first name to get started: kok Here is player one's game name: okkay7 (1)Which bible character had strength in his hair? a. Samuel b. Peter c.Samson d.John a Incorrect (1)Which bible character had strength in his hair? a. Samuel b. Peter c.Samson d.John c (2)King David danced out of his ______? So this is my output. I would like it to score the first answer then continue to the 2nd question and multiple choice display. – Korene Stuart Mar 08 '18 at 04:09
  • Unlike your comment, your code returns true only when the answer is 'a' or 'a.'. You need to change that to match with the respective answers to your questions. But I have modified the code to ask the same question until the user gives a correct answer. You can add the score increment/decrement yourself based on how you evaluate the answers. – hungersoft Mar 08 '18 at 04:18