gpt4 book ai didi

python - 求10名学生的平均成绩,并增加学生

转载 作者:行者123 更新时间:2023-12-01 03:44:34 25 4
gpt4 key购买 nike

我需要询问 10 名学生的最终成绩(同时)增加他们的值:

所以类似:

Please enter final grade for student 1

Please enter final grade for student 2

依此类推...直到10点

然后我需要获取他们输入的成绩,并找到平均值。

这是我到目前为止所拥有的:

def main():

x = []

for i in range(10):
final_grades = x.append(int(input('Please enter final grade for student: ')))


##average_final_grade = final_grades / 10

##print(average_final_grade)


main()

最佳答案

# list of grades
x = []
# count of students
n = 10
# fill list with grades from console input
# using pythonic generator
x = [int(input('Please enter final grade for student {}: '.format(i+1))) for i in range(n)]
# count average,
# sum is builtin way to sum values in list
# float required for python 2.x to make avg float, not int
average_final_grade = sum(x) / float(n)
print('Avg grade is {}'.format(average_final_grade))

Online demo .

关于python - 求10名学生的平均成绩,并增加学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39135410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com