制作一个统计计划 [英] Making a statistics program

查看:214
本文介绍了制作一个统计计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个计算并打印下面的Python程序:


  • 从分数列表的平均分数

  • 从分数的列表
  • 的最高得分
  • 谁得分最高的学生的名字。

该方案通过要求用户输入病例数开始。对于每一个情况下,程序应该要求用户输入学生的数量。对于每一个学生的程序要求用户输入学生的名字和标志。每个个案的程序报告的平均分,最高分和谁得到了最高分学生的名字。

另外
如果有一个以上的人用的情况下的最高分,该方案应仅报告第一次出现。
平均分和最高分应该有精确小数点后2位。
输出应该是作为示例程序的输出。

我一直在努力,到目前为止是这样的:

 等级= []
NAME_LIST = []
案件= INT(输入('输入病例数:'))
为的情况下在范围(1,例+ 1):
    打印(案例,案例)
    数= INT(输入('请输入学生人数:'))
    有效范围内的数字(1,号码+ 1):        名称= STR(输入(学生的输入名称:'))
        name_list.append(名)
        马克=浮动(输入('请输入学生马克:'))
        grade.append(标记)
        最高= MAX(级)
        平均=(SUM(级)/数字)
        high_name = grade.index(MAX(级))
        打印(平均,平均值)
        打印('最高',最高)
        打印(high_name)

这是我迄今破译。我现在最大的问题越来越与高分的人的名字。任何想法和反馈意见是非常AP preciated。由于相对于贴在下面的答案,恐怕我不明白的唯一事情就是字典功能,但另有其余的没有道理给我。


解决方案

  numcases = INT(输入(多少情况下,有?有))案件=名单()对于在_范围(numcases):
    #中,_用来表示我们不关心我们的数量
    #和范围(3)== [0,1,2]所以我们会得到相同数量的项目,我们投入    案例=字典()#实例化一个字典    对于_范围内(INT(输入(有多少学生在这种情况下?))):
        #相同的,因为我们没有过,但跳过一步
        名称=输入(学生姓名)
        得分=输入(学生的分数:)
        案例[名] =#比分扳平比分的名字    #在执行这一点上,这种情况下,所有数据应该是
    #保存在字典`case`键,所以...
    cases.append(情况)#我们钉了到我们的案件清单!#一旦我们到达这里,我们已经完成了对每一种情况,所以现在`cases`是
#我们有每种情况下的清单。对于案件的情况:
    MAX_SCORE = 0
    max_score_student =无#我们将在稍后需要这个
    total_score = 0,我们实际上并不需要这个,但它更容易解释
    NUM_ENTRIES = 0,我们实际上并不需要这个,但它更容易解释
    学生在案例:
        分数=案[学生]
        如果比分> MAX_SCORE:
            MAX_SCORE =得分
            max_score_student =学生        total_score + =得分
        NUM_ENTRIES + = 1
        #再次,我们不需要这些,但它有助于证明!!
    #当我们离开这个循环,我们就知道了最高分与学生
    #我们也总保存在`total_score`和`num_entries`长度
    #所以现在我们需要做的.....
    平均= total_score / MAX_ENTRIES    #然后再打印我们使用字符串格式化    打印(得分最高的是{} MAX_SCORE由{} max_score_student录。格式(
        MAX_SCORE = MAX_SCORE,max_score_student = max_score_student))
    打印(平均得分为:{}平均水平。格式(平均值=平均值))

I am trying to write a Python program that computes and prints the following :

  • the average score from a list of scores
  • the highest score from a list of scores
  • the name of the student who got the highest score.

The program starts by asking the user to enter the number of cases. For EACH case, the program should ask the user to enter the number of students. For each student the program asks the user to enter the student's name and marks. For EACH case the program reports the average marks, the highest marks and the name of the student who got the highest marks.

Also If there are more than one person with the highest score in a CASE, the program should report the first occurrence only. The average score and the highest score should have exactly 2 decimal places. The output should be as in the sample program output.

What I have been trying so far is the following:

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)

This is what i have deciphered so far. my biggest problem now is getting the name of the individual with the high score. Any thoughts and feedback is much appreciated. As with respect to the answer posted below, i am afraid the only thing i do not understand is the dictionary function but otherwise the rest does make sense to me.

解决方案

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))

这篇关于制作一个统计计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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