我有一个测试平均计算器,用户可以输入想要的测试分数(以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'))
答案 0 :(得分: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'))