【问题标题】:Calculating score for quiz计算测验分数
【发布时间】:2021-01-08 00:16:37
【问题描述】:

几乎只是一个编程爱好者。最近一直在尝试通过缩短我的代码来提升我的编程游戏。对于这个例子来说,数组确实会有所帮助,但我还没有锁定它们。

下面我做了自制测验。如果您运行脚本,您会看到它会提示您回答问题。然后,在回答时告诉你它是正确的还是错误的。我无法解决的问题是我的变量“正确”输出为零,无论函数中的 if 语句如何。我将它定义为零以从某个地方开始,但不明白为什么它没有添加到最终语句中。

任何帮助将不胜感激。代码如下:

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

answer = input("How many stars are in the sky?\nA. 100\nB. 1000\nC.10000\nD.Nobody knows.\n\nAnswer: ")
answerD()

answer = input("People who chew with their mouth open are what?\nA. Nice\nB. Detestable\nC. Considerate\nD. Mannerly\n\nAnswer: ")
answerB()

answer = input("which Star wars movie is a movie?\nA. Rise of the Sith\nB. Skywalker Strikes Back\nC. Revenge of the Sith\nD. Return of the Emperor\n\nAnswer: ")
answerC()

answer = input("Question 3: Which word below is spelled INCORRECTLY?\nA. Acommodate\nB. Pharaoh\nC. Separate\nD. Occurrence\nAnswer: ")
answerA()

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

【问题讨论】:

  • 你应该阅读变量范围:本地 vs 全局...
  • 在每个答案中,如果正确,则将正确的计数器增加 1,否则将其重置为 0。无论以前的答案如何,如果最后一个错误将是 0。从每个答案检查中删除“正确 = 0”作为起点。

标签: python


【解决方案1】:

您在每个if 语句之后将correct 设置为0。即使删除它也不能解决问题,因为它会引发另一个错误。

UnboundLocalError: local variable 'correct' referenced before assignment.

把你的代码改成这个 -

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

correct = 0        
answer = input("How many stars are in the sky?\nA. 100\nB. 1000\nC.10000\nD.Nobody knows.\n\nAnswer: ")
correct += answerD()

answer = input("People who chew with their mouth open are what?\nA. Nice\nB. Detestable\nC. Considerate\nD. Mannerly\n\nAnswer: ")
correct += answerB()

answer = input("which Star wars movie is a movie?\nA. Rise of the Sith\nB. Skywalker Strikes Back\nC. Revenge of the Sith\nD. Return of the Emperor\n\nAnswer: ")
correct += answerC()

answer = input("Question 3: Which word below is spelled INCORRECTLY?\nA. Acommodate\nB. Pharaoh\nC. Separate\nD. Occurrence\nAnswer: ")
correct += answerA()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2020-09-21
    • 2015-05-25
    • 2017-01-31
    相关资源
    最近更新 更多