【问题标题】:How to track the number of scores a user has input?如何跟踪用户输入的分数?
【发布时间】:2019-10-12 17:38:39
【问题描述】:

我有一个测试平均值计算器,用户可以输入他们想要多少测试分数(以 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】:

    您已经有了变量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'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多