如何跟踪用户输入的分数?

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

我有一个测试平均计算器,用户可以输入想要的测试分数(以btwn 1-100为单位)。我只需要跟踪用户输入的测试分数即可。我将如何实施?

#Welcome Message
print("Welcome to the Test Average Calculator!")
print("Say 'STOP' when you are done with Data Entry")

#Variables
total = 0
total_quiz = 0
inpt = input("Enter score: ")

#User Input
while inpt.upper() != "STOP":
    if int(inpt) <= 100 and int(inpt) > 0:
        total += int(inpt)
        total_quiz += 1
    else:
        print("Invalid Score")
    inpt = input("Enter Score or Stop?: ")

#Display Average
print('The Average score is: ',
      format(average, '.2f'))
python-3.x average
1个回答
0
投票

您已经有了变量total_quiz,它保留分数的数量,因此要计算平均值,您需要做的就是在打印结果前加average = total / total_quiz

#Welcome Message
print("Welcome to the Test Average Calculator!")
print("Say 'STOP' when you are done with Data Entry")

#Variables
total = 0
total_quiz = 0

while True:
    inpt = input("Enter score: ")

    if inpt.upper() == "STOP":
        break

    if 0 <= int(inpt) <= 100:
        total += int(inpt)
        total_quiz += 1
    else:
        print("Invalid Score")

average = total / total_quiz

# Display Average
print('The Average score is: ', format(average, '.2f'))
© www.soinside.com 2019 - 2024. All rights reserved.