用python计算学生/班级平均数

2024-04-29 01:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

Python3.3.3

我想写一个上课的程序,但我迷路了。这是我需要做的。

我需要根据输入的成绩计算每个学生的平均分。 我需要计算一个班级平均数。 如果学生进入-1年级,则停止输入分数。 需要打印每个学生年级的信息。 学生成绩应显示数字成绩和字母成绩。 这封信将以学生的信件成绩为准。

如何收集和存储学生姓名和考试成绩。 这样我就可以一次输出到显示学生姓名的地方。 一个数字平均数,一个基于该平均数的字母等级, 一份基于他们收到的字母等级的声明?

到目前为止,我的代码如下:

def main():
    another_student = 'y'
    while another_student == 'y' or another_student == 'Y':
        student_average()
        print()
        another_student = input('do you have another student to enter (y/n) ? ')
    while another_student == 'n' or another_student == 'N':
        student_average_list()
        class_average()
        break

def student_average():
    total = 0.0
    print()
    student_name = input('what is the students name? ')
    print()
    print()
    print(student_name)
    print('-------------------')
    number_of_tests = int(input('please enter the number of tests : '))
    for test_num in range(number_of_tests):
        print('test number', test_num + 1, end='')
        score = float(input(': '))
        total += score
    student_average = total / number_of_tests
    print ()
    print(student_name,"'s average is : ",student_average, sep='')

def student_average_list():
    print ('kahdjskh')

def class_average():
    print ('alsjd')

main()

Tags: ofnamenumberinputdef字母anothertests
2条回答

我想这已经接近你想要的了。它定义了一个Student类,使数据存储和处理更易于管理。

class Student(object):
    def __init__(self, name):
        self.name, self.grades = name, []

    def append_grade(self, grade):
        self.grades.append(grade)

    def average(self):
        return sum(self.grades) / len(self.grades)

    def letter_grade(self):
        average = self.average()
        for value, grade in (90, "A"), (80, "B"), (70, "C"), (60, "D"):
            if average >= value:
                return grade
        else:
            return "F"

def main():
    print()
    print('Collecting class student information')
    a_class = []  # "class" by itself is a reserved word in Python, avoid using
    while True:
        print()
        print('{} students in class so far'.format(len(a_class)))
        another_student = input('Do you have another student to enter (y/n) ? ')
        if another_student[0].lower() != 'y':
            break
        print()
        student_name = input('What is the student\'s name? ')
        a_class.append(Student(student_name))
        print()
        print('student :', student_name)
        print('-------------------')
        number_of_tests = int(input('Please enter the number of tests : '))
        for test_num in range(1, number_of_tests+1):
            print('test number {}'.format(test_num), end='')
            score = float(input(' : '))
            if score < 0:  # stop early?
                break
            a_class[-1].append_grade(score) # append to last student added

    print_report(a_class)

def print_report(a_class):
    print()
    print('Class Report')
    print()
    for student in sorted(a_class, key=lambda s: s.name):
        print('student: {:20s} average test score: {:3.2f}  grade: {}'.format(
            student.name, student.average(), student.letter_grade()))
    print()
    print('The class average is {:.2f}'.format(class_average(a_class)))

def class_average(a_class):
    return sum(student.average() for student in a_class) / len(a_class)

main()

你需要为全班保留一份分数表。student_average函数做的事情太多了。也许做一个函数get_student_marks,它只返回学生的分数列表。你需要一个average函数来计算一个列表的平均值,你可以用它来计算学生平均值和班级平均值。祝你好运!

相关问题 更多 >