0

I am rather new to Python. I am trying to create a self grading quiz. so far I only have two questions. I tried to assign a global variable for the number that the person gets right and the number of questions the person gets wrong. The program works correctly if all the correct answers are typed in; however any incorrect answer comes back as incorrect not being defined.

def test():
    print "We are going to take a small quiz"
    print "Lets start with what your name."
    name = raw_input("What is your name?")
    correct = [0]
    incorrect = [0]
    def q1():
            print "What organization did Hellboy work for?"
            answer = raw_input("Type your answer.").lower()
            if answer == "bprd" or answer == "b.p.r.d":
                    print "Your are correct."
                    correct[0] += 1
            else:
                    print "That is wrong."
                    incorret[0] += 1
    q1()
    def q2():
            print "What is the name of Captain America's sidekick?"
            answer = raw_input("Your answer please.").lower()
            if answer =="bucky barnes" or answer == "falcon":
                    print "Your are right"
                    correct[0] += 1
            else:
                    print "Sorry that in incorrect"
                    incorrect[0] += 1
    q2()



    print "Good job %s, you got %r questions right and %r questions wrong" % (name, correct, incorrect)
    raw_input()
test()
1
  • Specify your question, and let us know what you've tried. Commented Jan 15, 2014 at 3:01

2 Answers 2

2

You are getting undefined error because of a spelling mistake on this line in the first question:

incorret[0] += 1

Correct this and the code should work.

Sign up to request clarification or add additional context in comments.

Comments

-1

If you set it up as a class it might be easier.

class classTest():

    def __init__():
        print "We are going to take a small quiz"
        print "Lets start with what your name."
        self.name = raw_input("What is your name?")
        self.correct = 0
        self.incorrect = 0

    def q1(self):
        print "What organization did Hellboy work for?"
        answer = raw_input("Type your answer.").lower()
        if answer == "bprd" or answer == "b.p.r.d":
            print "Your are correct."
            self.correct += 1
        else:
            print "That is wrong."
            self.incorrect += 1

    def q2(self):
        print "What is the name of Captain America's sidekick?"
        answer = raw_input("Your answer please.").lower()
        if answer =="bucky barnes" or answer == "falcon":
            print "Your are right"
            self.correct[0] += 1
        else:
            print "Sorry that in incorrect"
            self.incorrect[0] += 1
    def end(self):
        print "Good job %s, you got %r questions right and %r questions wrong" % (self.name, self.correct, self.incorrect)

test = new classTest()
test.q1()
test.q2()
test.end()

Comments

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.