Question

I am doing an assignment where I need to conduct a quiz. This is a part of my code so far.

answer = input("Your answer: ")
guessesTaken = 0
points = 0
if answer == 'Call Of Duty' or answer == 'Call of duty' or answer == 'Call of duty' or answer == 'a)' or answer == 'call of duty' or answer == 'a':
    print("You are correct!")
    points = points + 2
    print("You scored two points!")
else: 
    guessesTaken = guessesTaken + 1
    print("Incorrect!")
    print("You have", guessesTaken, "guess remaining!")
answerb = input("Your answer: ")
if answerb == 'Call Of Duty' or answerb == 'Call of duty' or answerb == 'Call of duty' or answerb == 'a)' or answerb == 'call of duty' or answerb == 'a':
    points = points + 1
    print("You scored one point!")

I am getting an error for if answerb. I cannot figure out why I am getting this error. This part of the code is meant to score points for each question you get right. Points system is as follows, 2 for correct answer on first try, 1 on second try and 0 for third try. I thought using answerb would let me have a second try and give 1 point. If you could please explain it to me so I don't do it in the future again. :)

Was it helpful?

Solution

Like minitech suggested, you may have just indented answerb = input(...) too far. I modified your code a little:

maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
while guessesTaken<maxGuesses:
    answer = input("Your answer: ")
    if answer.lower() == 'call of duty' or answer.lower() == 'a' or answer.lower() == 'a)':
        print("You are correct!")
        points = points + maxGuesses-guessesTaken
        print("You scored %d points!" %(maxGuesses-guessesTaken))
        break
    else:
        print("Incorrect")
        print("You have %d guesses remaining!" %(maxGuesses-guessesTaken-1))
        guessesTaken += 1

OTHER TIPS

The error in your comment indicates on line 28 you are checking a variable that has not been set. The matching line in your post appears to be 13. In line 12, you appear to be setting the variable. Maybe try pasting your post back to your editor and re try. The posted code appears to be setting the variable before it attempts to access it correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top