是的。首先,我已经为您修复并格式化了您的代码。
# This program will compute the average quiz grade for 5 students
print("Hello, this program is designed to compute the average quiz grades for the following students: Clark, Nicole, Kiran, Alex, and Erik")
students = ["Clark", "Nicole", "Kiran", "Alex", "Erik"]
for student in students:
print(student, ", What was your quiz grade?")
grades = input("My grade is: ")
到目前为止,您可以向每个学生询问成绩。一旦他们告诉您,您应该将其存储在内存中。一个简单的方法是使用一个列表(我假设你还没有学过字典)。
您可以在询问成绩之前创建一个新列表,然后将用户输入的每个新成绩添加到列表末尾。
# This program will compute the average quiz grade for 5 students
print("Hello, this program is designed to compute the average quiz grades for the following students: Clark, Nicole, Kiran, Alex, and Erik")
students = ["Clark", "Nicole", "Kiran", "Alex", "Erik"]
grades = [] # create an empty list to store the grades in
for student in students:
print(student, ", What was your quiz grade?")
grade = input("My grade is: ")
grades.append(int(grade)) # convert the grade to an integer number, then add it to the list
print("Here are the grades you entered: ", grades)
现在,我们需要计算列表的平均值。回顾初等数学,一组数字的平均值是数字的总和除以数字的个数。
在 Python 中,您可以使用内置函数 sum() 来计算数字的总和。您可以使用内置函数len() 来获取列表中的元素个数。
现在,您只需将总和除以长度即可。
average = sum(grades) / len(grades)
print("The average is:", average)
现在,为了获得最高等级,您可以使用内置函数max()。这将找到并返回数组中的最大数字。
你可以这样使用它。
highest = max(grades)
print("The highest grade is:", highest)
如果您想要组合代码:
# This program will compute the average quiz grade for 5 students
print("Hello, this program is designed to compute the average quiz grades for the following students: Clark, Nicole, Kiran, Alex, and Erik")
students = ["Clark", "Nicole", "Kiran", "Alex", "Erik"]
grades = [] # create an empty list to store the grades in
for student in students:
print(student, ", What was your quiz grade?")
grade = input("My grade is: ")
grades.append(int(grade)) # convert the grade to an integer number, then add it to the list
print("Here are the grades you entered: ", grades)
average = sum(grades) / len(grades)
print("The average is:", average)
highest = max(grades)
print("The highest grade is:", highest)