我用python做了一个计算器,它给了我学生成绩平均值和学科成绩平均值。下面的 是我如何需要我的输入和我的输出应该如何看的图片。python的学生平均成绩和科目成绩平均值计算器
我想使用嵌套列表和函数。我试过这个。
我用python做了一个计算器,它给了我学生成绩平均值和学科成绩平均值。下面的 是我如何需要我的输入和我的输出应该如何看的图片。python的学生平均成绩和科目成绩平均值计算器
我想使用嵌套列表和函数。我试过这个。
您可以轻松地用while
循环做到这一点,熊猫
import pandas
list_marks = {}
subjects = int(input("Number of subjects: "))
while(True):
name = input("Name of student (leave empty if finished): ")
if not name:
break
scores = []
for i in range(1, subjects + 1):
scores.append(int(input("Subject %d: " % i)))
list_marks[name] = scores
scores = pandas.DataFrame(list_marks)
print(scores)
# student1 student2 student3
# 0 100 90 80
# 1 100 90 60
print(scores.mean(axis=0))
# student1 100.0
# student2 90.0
# student3 70.0
# dtype: float64
print(scores.mean(axis=1))
# 0 90.000000
# 1 83.333333
# dtype: float64
或使用NumPy的:
import numpy
scoresarray = numpy.array(list(list_marks.values()))
print(numpy.mean(scoresarray, axis=0))
# [ 90. 83.33333333]
print(numpy.mean(scoresarray, axis=1))
# [ 100. 90. 70.]
试试这个代码
def calc(student_count):
list_marks = []
for i in range(int(student_count)):
student_name = input("name of student?")
sub1_score = input("Score of subject 1?")
sub2_score = input("Score of subject 2?")
average = (int(sub1_score) + int(sub2_score))/2
list_marks.append([student_name,sub1_score,sub2_score,average])
print (list_marks)
return list_marks
no_of_students = input("How many students?")
#no_of_subjects = input("How many subjects?")
calc(no_of_students)
这将产生你想要的输出为两个科目。
如果你有很多科目,由他们计数
我认为这是更实际的把所有的成绩在Excel或CSV文件,该行或列,其中可以读入蟒蛇它们求和,然后除以。要计算平均值,请查看numpy平均值。 – mikey