首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获得10名学生的平均成绩- python

获得10名学生的平均成绩- python
EN

Stack Overflow用户
提问于 2016-08-27 02:01:31
回答 1查看 4.6K关注 0票数 0

我有一个程序,要求用户输入一个学生网络mid,然后他们在5次作业中得到了什么成绩,加上他们在期中和期末的成绩。然后将它们相加并进行除法,得到以表格形式显示的学生平均成绩。

我需要做的是,再循环整个过程9次。因此,本质上,我将询问另外9个学生相同的输入,然后我将需要以表格的格式显示。

我的问题是,我将如何循环的过程,我现在有'x‘的数量,然后显示所有学生的平均数。

这是我现在的代码:

代码语言:javascript
复制
   # x holds the list of grades
   x = []

   # count of assignments 
   assignments = 5

   # Ask for a student ID from user
   NETID = int(input('Enter your 4 digit student NET ID: '))

   # fill list with grades from console input
   x = [int(input('Please enter the grade you got on assignment {}: '.format(i+1))) for i in     range(assignments)]

   midTermGrade = int(input('Please enter the grade you got on you Mid-Term: '))
   finalGrade = int(input('Please enter the grade you got on you Final: '))

   # count average, 
   average_assignment_grade = (sum(x) + midTermGrade + finalGrade) / 7 

   print()
   print('NET ID \t Average Final Grade')
   print('---------------------------------')

   for number in range(1):
      print(NETID, '\t\t', format(average_assignment_grade, '.1f'),'%')

main()

这就是它在控制台上的样子:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-27 02:19:46

你真的做了最难的部分。我不明白你为什么不能这么做。总之:

代码语言:javascript
复制
student_count = 5;
A = [student_count]

for id_student in range(student_count):    
    print("STUDENT #", id_student+1)

    # x holds the list of grades
    x = []

    # count of assignments 
    assignments = 5

    # Ask for a student ID from user
    NETID = int(input('Enter your 4 digit student NET ID: '))

    # fill list with grades from console input
    x = [int(input('Please enter the grade you got on assignment {}: '.format(i+1))) for i in     range(assignments)]

    midTermGrade = int(input('Please enter the grade you got on you Mid-Term: '))
    finalGrade = int(input('Please enter the grade you got on you Final: '))

    # count average, 
    average_assignment_grade = (sum(x) + midTermGrade + finalGrade) / 7 

    print()
    print('NET ID | Average Final Grade')
    print('---------------------------------')

    for number in range(1):
        print(NETID, " | ", format(average_assignment_grade, '.1f'),'%')

    A.append(average_assignment_grade);

grades_sum = sum(A)
grades_average = grades_sum / 5;
print("SUM OF ALL STUDENTS = " + grades_sum)
print("AVERAGE OF ALL STUDENTS = " + grades_average)

Update:如上面所建议的那样,您应该为单个学生创建一个函数,并在另一个学生中循环该函数,因为这不是一个编码服务--我不会为您这样做,但我认为您已经知道了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39176762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档