平均成绩计算器 [英] Grade Average Calculator

查看:44
本文介绍了平均成绩计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在研究的问题:

Here is the question I am working on :

你想知道你在计算机科学方面的成绩,所以写一个程序不断地将 0 到 100 之间的等级评定为标准输入直到您输入 "stop" ,此时它应该打印您的平​​均值到标准输出.

You want to know your grade in Computer Science, so write a program that continuously takes grades between 0 and 100 to standard input until you input "stop" , at which point it should print your average to standard output.

这是迄今为止我在 Python 3 中的代码:

Here is my code thus far in Python 3:

total = 0
q = 1
score = input("Enter a score:")
while score != "stop":
    q += 1
    total = total + int(score)
avg = total / q
print(avg)

我对编码非常陌生,可以使用一些帮助来为我指明正确的方向.我觉得学习对我来说很重要,所以没有人应该觉得有义务只给我正确的答案.

I am very new at coding, and could use some help to point me in the right direction. I feel its important for me to learn, so nobody should feel obligated to just hand me the correct answer.

推荐答案

我会给你一个完整的大纲,以帮助你在算法上完成这个过程.

I'll give you a thorough outline to help you with this process algorithmically.

  • 变量总数为 0,变量计数为 0,并使用空输入字符串.
  • 创建一个 while 循环,该循环一直运行到输入变量是单词 stop 为止.
  • 将输入作为整数添加到总数中.计数加一.再次输入.
  • 循环外将总数除以浮点数除以累积计数.

现在让我们尝试将这个算法放在代码中.像我在下面做的那样.

Now let's try putting this algorithm together in code. Do as I do down below.

# Code Block 1

count = 0  # count variable
total = 0  # total variable

enter = '' # input variable

while enter != 'stop':
    enter = input('Enter a grade:' )

    if enter != 'stop' and enter.isdigit():
        total += int(enter) # add to total value
        count = count + 1   # then to the count

print float(total) / count




# Code Block 2

numbers = []

while enter != 'stop':
    enter = input('Enter a grade:' )

    if enter != 'stop':
        numbers.append(int(enter))

print sum(numbers) / float(len(numbers))

这篇关于平均成绩计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆