简体   繁体   English

如何使用信号值在while循环中平均用户输入?

[英]How to average user inputs with while loop using a sentinal value?

For a classroom assignment I have been asked to create a program that averages a number of user input exam grades using a while loop. 对于课堂作业,我被要求创建一个程序,该程序使用while循环对多个用户输入的考试成绩进行平均。 I have come up with a functioning program, however it does not meet the specific criteria required by my instructor. 我想出了一个可以运行的程序,但是它不符合我的老师要求的特定标准。 I need to Use a while-loop to allow the user to enter any number of exam scores, one number per line, until a sentinel value is entered. 我需要使用while循环来允许用户输入任意数量的考试分数,每行一个数字,直到输入前哨值。 How might I alter this program to get the same result using a while loop and a sentinel value of 9999? 我如何使用while循环和9999的前哨值​​来更改此程序以获得相同的结果?

here is what I have so far. 这是我到目前为止所拥有的。

scores=int(input("how many test scores will you enter: "))
total_sum=0
for n in range(scores):
    numbers=float(input("Enter exam score : "))
    total_sum+=numbers
avg=total_sum/scores
print("average of ", scores, " test scores is :", avg)

The output should look something like this 输出应如下所示

Enter exam score. 9999 to quit: 100
Enter exam score. 9999 to quit: 95.5
Enter exam score. 9999 to quit: 90
Enter exam score. 9999 to quit: 9999

These 3 scores average to : 95.16666667

Think the best way to do it would be store the scores in a list and then compute the average of them after the user enters the sentinel value: 认为最好的方法是将分数存储在列表中,然后在用户输入前哨值之后计算它们的平均值:

SENTINEL = float(9999)

scores = []
while True:
    number = float(input("Enter exam score (9999 to quit): "))
    if number == SENTINEL:
        break
    scores.append(number)

if not scores:
    print("You didn't enter any scores.")
else:
    avg = sum(scores)/len(scores)
    print("average of ", len(scores), " test scores is :", avg)

There are a couple ways to go about this. 有几种方法可以解决此问题。 The general approach would be to keep a running total of the scores, as well as how many scores you've read, and then check whether the next score to read is == 9999 to see whether or not to exit the while loop. 通用方法是保持所有分数的总和以及已阅读的分数,然后检查下一个要读取的分数是否== 9999以查看是否退出while循环。

A quick version might be the following: 快速版本可能如下:

num_scores = 0
total_sum = 0
shouldExit = False
while shouldExit is False:
    nextScore = float(input("Enter exam score : "))
    if nextScore == 9999: #find a way to do this that does not involve a == comparison on a floating-point number, if you can
        shouldExit = True
    if shouldExit is False:
        num_scores += 1
        total_sum  += nextScore
avg = total_sum / num_scores

See how that sort of approach works for you 看看这种方法如何为您工作

You can just break out of your for loop after getting input. 输入后,您可以中断for循环。

for n in range(scores):
    c = int(Input('Enter test score, enter 9999 to break'))
    if c == 9999:
       break;
    scores += c

Something like that at least. 至少这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM