Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 求for循环中变量的不同迭代次数的平均值_Python_Loops_For Loop_Average - Fatal编程技术网

Python 求for循环中变量的不同迭代次数的平均值

Python 求for循环中变量的不同迭代次数的平均值,python,loops,for-loop,average,Python,Loops,For Loop,Average,我似乎无法理解这一点——我想在程序结束时得到所有输入的平均值,但我不知道如何在循环的每次迭代中保存输入。任何帮助都将不胜感激,谢谢 students = int(input("How many students are in your class?")) while students <= 0: print ("Invalid # of students. Try again.") students = int(input("How many students are i

我似乎无法理解这一点——我想在程序结束时得到所有输入的平均值,但我不知道如何在循环的每次迭代中保存输入。任何帮助都将不胜感激,谢谢

students = int(input("How many students are in your class?"))

while students <= 0:
    print ("Invalid # of students. Try again.")
    students = int(input("How many students are in your class?"))

studentnum = 1 

for x in range (0, students):
    print ("*** Student", studentnum, " ***")
    score1 = int(input("Enter score for test #1"))
    while score1 < 0:
            print ("Invalid score. Try again.")
            score1 = int(input("Enter score for test #1"))
    score2 = int(input("Enter score for test #2"))
    while score1 < 0:
            print ("Invalid score. Try again.")
            score1 = int(input("Enter score for test #1"))
    part1 = score1 + score2
    av = part1 / 2
    print ("Average score for student #", studentnum, "is", av)
    studentnum = studentnum + 1

# figure out how to average out all student inputs
students=int(输入(“你们班有多少学生?”)

当学生时,您可以为每个学生建立一个平均分数列表(通过在每次循环迭代中附加到该列表),然后在退出循环后找到平均分数:

student_average_scores = []

for student in xrange(students):
    # <your code that gets av>
    student_average_scores.append(av)

average_score = sum(student_average_scores) / float(len(student_average_scores))
学生平均分=[]
对于xrange中的学生(学生):
# 
学生平均分数。附加(av)
平均分数=总和(学生平均分数)/浮动(len(学生平均分数))

只需创建一些东西将它们存储在循环外部即可

scores = []
for x in range (0, students):
   ...
   scores.append({'score1': score1, 'score2': score2, 'avg', av})

total_avg = sum(d['avg'] for d in scores) / len(scores)

你是说
student1score1
student1score2
student2score1
等的平均值?有一个输入错误,
score2
之后无效分数的错误捕获正在检查
score1
为什么在for循环中有两次相同的while循环?@K.Menyah-因为该循环是复制粘贴的,没有将
score1
更新为
score2
。或者,您可以执行
total=0;while stuff:total+=av;total/students
,这样您就不会在中间列表中存储任何内容。