Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

python - Calculating score for quiz

Pretty much just a programming hobbyist. Recently been trying to up my programming game by shortening my code. Arrays would really help with that for this example, but I still haven't gotten them locked in yet.

Below I made homemade quiz. If you run the script you'll see it will prompt the question for you to answer. Then, tell you if it is correct or incorrect upon answering. The problem I can't solve is my variable "correct" outputs zero regardless of my if statements in the functions. I defined it as zero to start somewhere but don't understand why it isn't adding to the final statement.

Any help would be appreciated. Code below:

    correct = 0
def answerA():
    if answer == 'a':
        correct = 0
        correct += 1
        print("Correct
")
    else:
        print("Incorrect
")
def answerB():
    if answer == 'b':
        correct = 0
        correct += 1
        print("Correct
")
    else:
        print("Incorrect
")
def answerC():
    if answer == 'c':
        correct = 0
        correct += 1
        print("Correct
")
    else:
        print("Incorrect
")
def answerD():
    if answer == 'd':
        correct = 0
        correct += 1
        print("Correct
")
    else:
        print("Incorrect
")

answer = input("How many stars are in the sky?
A. 100
B. 1000
C.10000
D.Nobody knows.

Answer: ")
answerD()

answer = input("People who chew with their mouth open are what?
A. Nice
B. Detestable
C. Considerate
D. Mannerly

Answer: ")
answerB()

answer = input("which Star wars movie is a movie?
A. Rise of the Sith
B. Skywalker Strikes Back
C. Revenge of the Sith
D. Return of the Emperor

Answer: ")
answerC()

answer = input("Question 3: Which word below is spelled INCORRECTLY?
A. Acommodate
B. Pharaoh
C. Separate
D. Occurrence
Answer: ")
answerA()

print("Answers correct: " + str(correct))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are setting correct to 0 after every if statement. Even removing it won't solve the issue as it will throw another error.

UnboundLocalError: local variable 'correct' referenced before assignment.

Change your code to this -

def answerA():
    if answer == 'a':
        print("Correct
")
        return 1
    else:
        print("Incorrect
")
        return 0
def answerB():
    if answer == 'b':
        print("Correct
")
        return 1
    else:
        print("Incorrect
")
        return 0
def answerC():
    if answer == 'c':
        print("Correct
")
        return 1
    else:
        print("Incorrect
")
        return 0  
def answerD():
    if answer == 'd':
        print("Correct
")
        return 1
    else:
        print("Incorrect
")
        return 0

correct = 0        
answer = input("How many stars are in the sky?
A. 100
B. 1000
C.10000
D.Nobody knows.

Answer: ")
correct += answerD()

answer = input("People who chew with their mouth open are what?
A. Nice
B. Detestable
C. Considerate
D. Mannerly

Answer: ")
correct += answerB()

answer = input("which Star wars movie is a movie?
A. Rise of the Sith
B. Skywalker Strikes Back
C. Revenge of the Sith
D. Return of the Emperor

Answer: ")
correct += answerC()

answer = input("Question 3: Which word below is spelled INCORRECTLY?
A. Acommodate
B. Pharaoh
C. Separate
D. Occurrence
Answer: ")
correct += answerA()

print("Answers correct: " + str(correct))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...