python使用测试分数列表

标签 python

我需要一些关于如何完成这个程序的建议。可能有一种更简洁、更流线型的方式来编写它,因此我们将不胜感激。以下是说明:

创建一个程序,对类(class)中的最终成绩进行分析。程序必须使用循环并将每个等级添加到列表中。该程序要求用户输入 10 名学生的最终成绩(整数百分比)。然后程序将显示以下数据:

  • 全类最高分。
  • 全类最低分。
  • 全类平均分

  • 这是我到目前为止所拥有的。当我运行它时,我的平均值是错误的。另外我在想有某种范围或索引函数来做循环部分?如果您想添加另一个分数,我希望它不要在每次输入后询问。
    def main():
        scores = get_scores()
        total = get_total(scores)
        average = total/len(scores)
        my_list = [get_scores]
        print('The lowest score is: ', min(scores))
        print('The highest score is: ', max(scores))
        print('the average is: ', average)
    
    def get_scores():
        test_scores = []
        again = 'y'
            while again == 'y':
            value = float(input('Enter score: '))
            test_scores.append(value)
            print('Do you want to enter another score?')
            again = input('y =yes, anything else = no: ')
            print()
        return test_scores
    
    def get_total(value_list):
        total = 0.0
        for num in value_list:
            total += num
            return total
    
    main()
    

    感谢您花时间看这个。欢迎任何帮助,我是编程新手,从您的所有反馈中学到了很多东西。

    感谢大家的所有反馈。我把你所有的建议都拼凑起来了。
    def main():
        scores = get_scores()
        total = get_total(scores)
        average = total/len(scores)
        my_list = [get_scores]
        print('The lowest score is: ', min(scores))
        print('The highest score is: ', max(scores))
        print('the average is: ', average)
    
    def get_scores():
        test_scores = []
        for scores in range(10):
            value = float(input('Enter score: '))
            test_scores.append(value)
        return test_scores
    
    def get_total(value_list):
        total = 0.0
        for num in value_list:
            total += num
        return total
    

    再次感谢大家的帮助和建议。这东西对我来说越来越难了,但我会继续努力 :) 谢谢大家 o/
    main()
    

    最佳答案

    你的问题是 return total缩进太远 - 它在 for循环,所以它只在第一个项目添加到列表后返回。它只是对内置 sum 的重新实现功能,所以你可以完全摆脱它。我厌倦了一直按“y”,所以当我在这里时,提示符变稀了。

    def main():
        scores = get_scores()
        total = sum(scores)
        average = total/len(scores)
        print('The lowest score is: ', min(scores))
        print('The highest score is: ', max(scores))
        print('the average is: ', average)
    
    def get_scores():
        test_scores = []
        print("Enter scores, one per line, terminating with an empty line")
        while True:
            entry = input('> ').strip()
            if not entry:
                break
            try:
                test_scores.append(float(entry))
            except ValueError:
                print("ERROR: {!r} is invalid, try that one again.".format(entry))
        return test_scores
    
    main()
    

    关于python使用测试分数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43214120/

    相关文章:

    python - 无法更改 _NET_WM_STRUT_PARTIAL 属性

    python - 使用 Python 请求时,Azure DevOps REST API 返回 HTTP 状态 203

    python - 为什么 Python 允许解压空的可迭代对象?

    python - 使用示例代码时出现 Pymongo pymongo.errors.ServerSelectionTimeoutError

    python - Django Rest Framework 和 Angular 2 文件上传

    python - 按分位数对 Pandas 数据框进行排名

    python - 值错误 : x and y must have same first dimension

    Python - 将字节解包为 ascii 字符但带有重音符号

    python - .join 在 python 中可能存在空队列

    python - 透视数据框,索引列中的列保持不变