我打算编写一个测验程序,当答案正确时,将分数相加,当用python给出错误答案时,将分数扣除

我打算编写一个测验程序,当答案正确时,将分数相加,当用python给出错误答案时,将分数扣除,python,Python,这就是我想出来的,我不明白为什么会这样,因为我的期望是每一个正确的答案都要加分,每一个错误的答案都要扣分 name = str(input("What is your name?: ")) score = 0 answer1 = str(input("(1) What is the name of the first president of Nigeria? \n(a) Amadu Bello \n(b) Shehu Shagari \n(c) Olusegun Obasanjo \n(d)

这就是我想出来的,我不明白为什么会这样,因为我的期望是每一个正确的答案都要加分,每一个错误的答案都要扣分

name = str(input("What is your name?: "))
score = 0

answer1 = str(input("(1) What is the name of the first president of Nigeria? \n(a) Amadu Bello \n(b) Shehu Shagari \n(c) Olusegun Obasanjo \n(d) Nnamdi Azikwe\n:Your answer: "))

if answer1 == "d" or "D" or "Nnamdi" or "Nnamdi Azikwe" and "Azikwe":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer2 = str(input("(2) What is the name of the capital of Lagos state? \n(a) Ikorodu\n(b) Ikeja\n(c) Surulere\n(d) Victoria Island\nYour answer: "))

if answer2 == "a" or "A" or "Ikeja":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer3 = str(input("(3) What is the name of the first capital of Nigeria?\n(a) Kano \n(b) Abuja\n(c) Lagos\n(d) Calabar\nYour Answer: "))

if answer3 == "c" or "C" or "lagos" and "Lagos":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer4 = str(input("(4) What is the name of the governor of Lagos when Code-Lagos was first implemented? \n(a)Raji Fashola\n(b)Akinwunmi Ambode\n(c) Ahmed Tinubu\n (d) Lateef Jakande\nYour answer: "))

if answer4 == "b" or "B" or "Ambode" or "Akinwunmi Ambode" or "akinwunmi ambode":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")


answer5 = str(input("(5) Which of the following is indigenous Nigerian content? \n(a) Etisalat\n(b) Airtel\n(c) MTN\n(d) Globacoms\n Your Answer: "))

if answer5 == "d" or "D" or "glo" or "Glo" or "GLO":
    score = score+5
    print ( "Weldone", name, "you've scored", score, "points")
else:
    score = score - 5
    print( "ouch!!!", name, "you missed that, you've scored, ", score, "points")

输出,只接受任何正确答案并加起来。

代码中的问题是在if条件下错误地使用了or运算符

通常,如果要对照值B和C检查变量a,则需要执行以下操作:

if a == B or a == C:
而不是简单地

if a == B or C
注意,在第二种情况下,它取B或C的值,这是一个布尔值,并将其与a进行比较,这显然不是您想要的

如果要将a与整个变量列表进行比较,可以执行以下操作:

if a in [B, C, D, ...]:
因此,在代码中,尝试替换以下内容:

if answer1 == "d" or "D" or "Nnamdi" or "Nnamdi Azikwe" and "Azikwe":
为此:

if answer1 in ["d", "D", "Nnamdi", "Nnamdi Azikwe", "Azikwe"]:
……等等

通过使用字典存储问题、选项和答案,您可以极大地消除代码中的冗余,如下所示:

questions = [
    {
        "question": "What is the name of the first president of Nigeria?",
        "options": ["Amadu Bello", "Shehu Shagari", "Olusegun Obasanjo", "Nnamdi Azikwe"],
        "answers": ["d", "nnamdi", "nnamdi azikwe", "azikwe"]
    },
# add more questions here in the same format as above
]

name = str(input("What is your name?: "))
score = 0

for q_number, q in enumerate(questions):
    question_text = '({}) {}\n'.format(q_number + 1, q["question"])
    options_text = '\n'.join("({}) {}".format(chr(i + 65), qsn) for i, qsn in enumerate(q["options"])) + "\n"
    display_text = question_text + options_text

    # answer is converted to lower case to make comparison with options easier
    answer = str(input(display_text)).lower()

    if answer in q["answers"]:
        score += 5
        print ("Well done", name, "you've scored", score, "points")
    else:
        score -= 5
        print("ouch!!!", name, "you missed that, you've scored, ", score, "points")

考虑给出一个而不是倾倒你所有的代码……来放大一下这里的错误,以及MCVE是如何导致一个更强有力的问题的。我们的目标是建立一个问题集和答案集,这对尽可能多的人是有帮助的。这意味着每一个问题都应该集中在它所包含的问题上,不相关的细节应该被忽略;只发布造成该问题的最短代码可以更容易地看到错误,从而更容易理解答案是如何修复的,因此其他犯相同错误的人可以更容易地看到相似性,并从您的问题答案中学习……同样,理想的标题也会关注您遇到的问题,这不是你遇到它时想要完成的事情——因为这是一个问题,答案对其他人来说是有用的;下一个在理解如何工作或如何工作方面犯同样错误的人可能不会自己也在做测试程序。谢谢Ashish,上面的部分,下面关于冗余的部分仍然在我的脑海中,因为我刚刚学习。但是谢谢你的回复,因为一切都很好。