Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 平均用户输入_Python - Fatal编程技术网

Python 平均用户输入

Python 平均用户输入,python,Python,第四项作业涉及编写一个Python程序来计算五名学生的平均测验分数。你的程序应该包括五个名字的列表。使用for循环,它应该连续提示用户为五个学生中的每一个提供测验分数。每个提示都应包括要输入其测验分数的学生的姓名。它应该计算并显示这五个等级和最高等级的平均值。你应该决定这五个学生的名字 这是我的作业,下面是我到目前为止的作业 # This program will compute the average quiz grade for 5 students print ("Hello,

第四项作业涉及编写一个Python程序来计算五名学生的平均测验分数。你的程序应该包括五个名字的列表。使用for循环,它应该连续提示用户为五个学生中的每一个提供测验分数。每个提示都应包括要输入其测验分数的学生的姓名。它应该计算并显示这五个等级和最高等级的平均值。你应该决定这五个学生的名字

这是我的作业,下面是我到目前为止的作业

# 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 = eval(input("My grade is: "))

该项目询问每个学生的成绩。但是,我需要把这些数字加起来,这样我就可以把它们除以平均值。有人能给我建议解决这个问题的正确方法吗?

是的。首先,我已经为您修复并格式化了代码

#此程序将计算5名学生的平均测验分数
print(“您好,本程序旨在计算以下学生的平均测验分数:克拉克、妮可、基兰、亚历克斯和埃里克”)
学生=[“克拉克”、“妮可”、“基兰”、“亚历克斯”、“埃里克”]
对于学生中的学生:
打印(学生,“你的测验分数是多少?”)
成绩=输入(“我的成绩是:”)
到目前为止,你可以要求每个学生给一个分数。一旦他们告诉你,你应该把它存储在内存中。一个简单的方法是使用一个列表(我假设你还没有学会字典)

您可以在请求成绩之前创建一个新列表,然后将每个新的a级用户输入添加到列表末尾

# 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=总和(等级)/len(等级)
打印(“平均值为:”,平均值)
现在,为了获得最高等级,您可以使用内置函数
max()
。这将查找并返回数组中的最大数字

你可以这样使用它

最高=最高(等级)
打印(“最高等级为:”,最高)
如果需要组合代码:

#此程序将计算5名学生的平均测验分数
print(“您好,本程序旨在计算以下学生的平均测验分数:克拉克、妮可、基兰、亚历克斯和埃里克”)
学生=[“克拉克”、“妮可”、“基兰”、“亚历克斯”、“埃里克”]
grades=[]#创建一个空列表以存储成绩
对于学生中的学生:
打印(学生,“你的测验分数是多少?”)
成绩=输入(“我的成绩是:”)
grades.append(int(grade))#将等级转换为整数,然后将其添加到列表中
打印(“这是您输入的成绩:”,成绩)
平均值=总和(等级)/长度(等级)
打印(“平均值为:”,平均值)
最高=最高(等级)
打印(“最高等级为:”,最高)