【问题标题】:Making a statistics program制作统计程序
【发布时间】:2014-03-08 13:01:08
【问题描述】:

我正在尝试编写一个 Python 程序来计算和打印以下内容:

  • 分数列表中的平均分数
  • 分数列表中的最高分
  • 获得最高分的学生的姓名。

程序首先要求用户输入病例数。对于每个案例,程序应要求用户输入学生人数。对于每个学生,程序要求用户输入学生的姓名和分数。对于每个案例,程序都会报告平均分、最高分和获得最高分的学生的姓名。

还有 如果一个 CASE 中有多个得分最高的人,则程序应仅报告第一次出现的情况。 平均分和最高分应精确到小数点后 2 位。 输出应该与示例程序输出中的一样。

到目前为止,我一直在尝试以下内容:

grade=[]
name_list=[]
cases=int(input('Enter number of cases: '))
for case in range(1,cases+1):
    print('case',case)
    number=int(input('Enter number of students: '))
    for number in range (1,number+1):

        name=str(input('Enter name of student: '))
        name_list.append(name)
        mark=float(input('Enter mark of student:'))
        grade.append(mark)


        highest= max (grade)
        average=(sum(grade)/number)
        high_name=grade.index(max(grade))
        print('average',average)
        print('Highest',highest)
        print (high_name)

这是我到目前为止所破译的。我现在最大的问题是获得高分的人的名字。非常感谢任何想法和反馈。至于下面发布的答案,恐怕我唯一不明白的是字典功能,但其余的对我来说确实有意义。

【问题讨论】:

  • 您认为将一个案例数量乘以现有案例列表会做什么?
  • 我希望将案例编号 * 列表相乘可以让我输入案例。例如:假设 case = 2 的输入,那么将为 2 个案例创建 2 个列表我稍后想要的是进一步探索这两个案例,以便我可以添加以下信息:案例 x 中的学生姓名, x 中学生的分数, 学生分数的平均值, 高分, 与高分相关的印刷姓名。然后转到案例 2,我已经重新启动了我的代码,现在就像这样:
  • case= int(input('输入案件数量')) case=[] for case in range (0,case+1): cases.append(case*cases) print (cases ) student=int(input('输入学生人数:')) students=[]
  • 你可以在 python 3.4+ docs.python.org/3.4/library/statistics.html使用新的 statistcs 模块
  • 你现在在high_name中已经有了最高等级的索引。最高年级和该年级学生在列表中的索引相同。

标签: python arrays python-3.x


【解决方案1】:

这类似于一项任务,它具体细节。

无论如何,official docs 是开始学习 Python 的好地方。 它们非常清晰,并且有很多有用的信息,例如

range(start, end):如果省略start参数,则默认为0

关于lists 的部分应该会给你一个良好的开端。

【讨论】:

  • 感谢您的反馈!!。我将前往上述区域尝试解决此问题。
【解决方案2】:
numcases = int(input("How many cases are there? "))

cases = list()

for _ in range(numcases):
    # the _ is used to signify we don't care about the number we're on
    # and range(3) == [0,1,2] so we'll get the same number of items we put in

    case = dict() # instantiate a dict

    for _ in range(int(input("How many students in this case? "))):
        # same as we did before, but skipping one step
        name = input("Student name: ")
        score = input("Student score: ")
        case[name] = score # tie the score to the name

    # at this point in execution, all data for this case should be
    # saved as keys in the dictionary `case`, so...
    cases.append(case) # we tack that into our list of cases!

# once we get here, we've done that for EVERY case, so now `cases` is
# a list of every case we have.

for case in cases:
    max_score = 0
    max_score_student = None # we WILL need this later
    total_score = 0 # we don't actually need this, but it's easier to explain
    num_entries = 0 # we don't actually need this, but it's easier to explain
    for student in case:
        score = case[student]
        if score > max_score:
            max_score = score
            max_score_student = student

        total_score += score
        num_entries += 1
        # again, we don't need these, but it helps to demonstrate!!
    # when we leave this for loop, we'll know the max score and its student
    # we'll also have the total saved in `total_score` and the length in `num_entries`
    # so now we need to do.....
    average = total_score/max_entries

    # then to print we use string formatting

    print("The highest score was {max_score} recorded by {max_score_student}".format(
        max_score=max_score, max_score_student=max_score_student))
    print("The average score is: {average}".format(average=average))

【讨论】:

  • +1 以获得良好的解释性 cmets。我认为你平均犯了一个小错误。应该除以num_entries 对吗?或许可以解释什么是 dict,或者是文档的链接。开始学习 python 的人可能还没有看过这些。
  • 惊人的代码。但是我仍然是初学者,所以我对 dict 函数的意义有些迷茫。我已经更新了我的代码,但我不知道如何以正确的格式在 stackoverflow 上发布
  • grade=[] name_list=[] case=int(input('Enter number of cases: ')) for case in range(1,cases+1): print('case',case ) number=int(input('Enter number of students: ')) for number in range (1,number+1): name=str(input('Enter name of student: ')) name_list.append(name) mark =float(input('输入学生的分数:'))grade.append(mark)highest=max(grade)average=(sum(grade)/number)high_name=grade.index(max(grade)) print(' average',average) print('Highest',highest) print (high_name) 我有...
  • 关联名字和高分的问题
  • @KingShahmoo 你可以edit你的问题并在那里添加那个客栈。
猜你喜欢
  • 1970-01-01
  • 2010-12-14
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多