【问题标题】:I intend to write a quiz program that adds up points when the answer is correct and deducts point when a wrong answer is given in python [duplicate]我打算编写一个测验程序,当答案正确时加分,在python中给出错误答案时减分[重复]
【发布时间】:2019-02-01 13:03:05
【问题描述】:

这是我想出的,我不明白为什么会这样,因为我的期望是每个正确答案都会增加分数,任何错误答案都会扣分。

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")

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

【问题讨论】:

  • 考虑给minimal reproducible example,而不是倾倒你所有的代码...
  • 为了详细说明这里出了什么问题(以及 MCVE 如何导致更强有力的问题)——我们的目标是建立对尽可能多的人有帮助的问题和答案的集合尽可能。这意味着每个问题都应该专注于它所包含的问题,而忽略不相关的细节;仅发布产生该问题的最短代码可以更轻松地查看错误,从而更容易理解答案如何修复它,因此犯相同错误的其他人可以更轻松地看到相似性并从您的问题的答案中学习。跨度>
  • ...类似地,一个理想的标题关注的是你遇到的问题,而不是你遇到它时想要完成的事情——因为这是对别人有用的问题将寻找;下一个在理解or 的工作原理方面犯同样错误的人可能不会自己也参与测验程序。

标签: python


【解决方案1】:

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

一般来说,如果您想检查变量 a 与值 BC,您需要这样做:

if a == B or a == C:

而不是简单地

if a == B or C

请注意,在第二种情况下,它采用布尔值 B or 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")

【讨论】:

  • 谢谢Ashish,对于上面的部分,处理冗余的下部分仍然在我头上,因为我刚刚学习。但是感谢您的回复,因为它运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多