如何在测验中添加计分器,如果正确,第二次尝试会减去 0.5 分

问题描述 投票:0回答:2

第一次使用 slack overflow 用户。我正在使用 python 学习初级 CS 课程,任务是创建一个测验,使用 if/else 结构检查用户答案的正确性。如果用户回答正确,则通知用户并给予满分。否则,允许用户第二次尝试,如果回答正确则提供一半分数,如果不正确则提供零分。我粘贴了我的测验中的两个问题作为示例。

score = 0

#Q1 Multiple Choice
for tries in range(2):
    print("\n", "QUESTION 1:", "\n", "Which of the following terms describes the body's ability to maintain its normal state? Input a Letter A-E ")
    print(" a) Anabolism", "\n", "b) Catabolism", "\n", "c) Tolerance", "\n", "d) Homeostasis", "\n", "e) Metabolism")
    answer1 = input("Make your choice: ")
    if answer1 == "D" or answer1 == "d" :
        print("Correct!")
        score = score + 1 # full points given if correct on first attempt
        break
    else:
        print("False!")
        score = score + 0.5 # 0.5 points deducted if correct on second attempt
else:
    print("Out of chances! Sorry you are incorrect, Homeostasis (answer D) describes the body’s ability to maintain its normal state.”")
    score = score + 0 # no points given if second attempt was wrong 
    
#Q2 Numeric Response
for i in range(2):
    print("QUESTION 2")
    answer = input("How many organ systems are in the human body? Please enter an integer:") 
    
    if answer == "11":
        print("Correct, there are 11 organ systems in the body.The 11 systems are Integumentary System, Skeletal System, Muscular System, Nervous System, Endocrine System, Cardiovascular System, Lymphatic System, Respiratory System Digestive System, Urinary System, and Reproductive System")
        score = score + 1
        break;
    else:
        print("Incorrect")
    if(i == 1):
        print("answer = 11")
        score = score + 0.5

print("you scored",score, "points")


我添加了分数=分数+1,它有效,但它不会在第二次尝试中减去0.5,而是将所有分数相加,而不管正确性如何。我尝试添加另一个 if else 语句来说明如果他们在第二次尝试后出错,则分配零分,如果他们正确则分配 0.5 但我相信有一种更简单的方法可以添加它,而无需使用一百万个 if else 语句,但我只是想不通。我没有收到任何错误消息,只是计算不正确,我相信这与我如何格式化它或我将分数计算放在打印语句下的位置有关。

python loops if-statement
2个回答
1
投票

每次他们出错时,你的代码都会加半分,即使他们在下一次尝试时没有答对。因此,如果他们错了两次,您将给他们两次半分,这与他们答对了一样。

只有当他们回答正确时,你才应该添加分数。然后在

tries
语句中使用
if
的值来确定添加多少。

    if answer1 == "D" or answer1 == "d" :
        print("Correct!")
        if tries == 0:
            score += 1
        else:
            score += 0.5
        break
    else:
        print("False!")

0
投票

您可以修改现有代码,例如:

score = 0

# Q1 Multiple Choice
for tries in range(2):
    print("\nQUESTION 1:\nWhich of the following terms describes the body's ability to maintain its normal state? Input a Letter A-E ")
    print("a) Anabolism\nb) Catabolism\nc) Tolerance\nd) Homeostasis\ne) Metabolism")
    answer1 = input("Make your choice: ").strip().upper()  # Ensure case insensitivity
    
    if answer1 == "D":
        print("Correct!")
        score += 1  # Full points given if correct on first attempt
        break
    else:
        print("False!")
        if tries == 1:
            print("Out of chances! Sorry you are incorrect, Homeostasis (answer D) describes the body’s ability to maintain its normal state.")
            break
        else:
            print("Second attempt.")
            score += 0.5  # 0.5 points deducted if correct on second attempt

# Q2 Numeric Response
for i in range(2):
    print("\nQUESTION 2:")
    answer = input("How many organ systems are in the human body? Please enter an integer: ").strip()

    if answer == "11":
        print("Correct, there are 11 organ systems in the body.")
        score += 1
        break
    else:
        print("Incorrect.")
        if i == 1:
            print("Answer = 11.")
            break
        else:
            print("Second attempt.")

print("\nYou scored", score, "points.")

输出:

QUESTION 1:
Which of the following terms describes the body's ability to maintain its normal state? Input a Letter A-E 
a) Anabolism
b) Catabolism
c) Tolerance
d) Homeostasis
e) Metabolism
Make your choice: b
False!
Second attempt.

QUESTION 1:
Which of the following terms describes the body's ability to maintain its normal state? Input a Letter A-E 
a) Anabolism
b) Catabolism
c) Tolerance
d) Homeostasis
e) Metabolism
Make your choice: d
Correct!

QUESTION 2:
How many organ systems are in the human body? Please enter an integer: 11
Correct, there are 11 organ systems in the body.

You scored 2.5 points.
© www.soinside.com 2019 - 2024. All rights reserved.