Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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/8/python-3.x/19.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 在嵌套循环中查找类avg_Python_Python 3.x_For Loop_While Loop - Fatal编程技术网

Python 在嵌套循环中查找类avg

Python 在嵌套循环中查找类avg,python,python-3.x,for-loop,while-loop,Python,Python 3.x,For Loop,While Loop,我正在做一个小的练习程序,它允许你输入3个测试分数,你想输入多少就输入多少,最后我想计算所有学生之间的平均值。我可以输入所有学生的名字和分数,它将返回他们的平均值,但当你输入“*”时,它只计算最后一个学生的平均值,我试图找出如何让它根据他们的考试分数计算所有学生的平均值 def GetPosInt(): nameone = str while nameone != "*": nameone =(input("Please en

我正在做一个小的练习程序,它允许你输入3个测试分数,你想输入多少就输入多少,最后我想计算所有学生之间的平均值。我可以输入所有学生的名字和分数,它将返回他们的平均值,但当你输入“*”时,它只计算最后一个学生的平均值,我试图找出如何让它根据他们的考试分数计算所有学生的平均值

def GetPosInt():     
        nameone = str

        while nameone != "*":

            nameone =(input("Please enter a student name or '*' to finish: "))

            if nameone != "*":
                scoreone = int(input("Please enter a score for " + nameone +": "))

                if scoreone < 0:
                    print("positive integers please!")
                    break

                else:
                    scoretwo = float(input("Please enter another score for "+nameone+": "))
                    scorethree = float(input("Please enter another score for "+nameone+": "))

                testscores = scoreone + scoretwo + scorethree
                avg = testscores / 3
                print("The average score for",nameone,"is ",avg)   
                classtotal = avg

            if nameone == "*":
                classavg = classtotal / 3
                print("The class average is: ",classavg)

# main
def main():
    GetPosInt()

main()
def GetPosInt():
nameone=str
当一个人"*":
nameone=(输入(“请输入学生姓名或“*”以完成:”)
如果一个人"*":
scoreone=int(输入(“请为“+nameone+”:”)输入分数)
如果分数一小于0:
打印(“请输入正整数!”)
打破
其他:
scoretwo=浮动(输入(“请为“+nameone+”:”)输入另一个分数)
分数三=浮动(输入(“请为“+nameone+”:”)输入另一个分数)
测试分数=分数一+分数二+分数三
平均得分=测试分数/3
打印(“nameone”的平均分为“is”,平均分)
classtotal=平均值
如果nameone==“*”:
classavg=classtotal/3
打印(“类平均值为:”,类平均值)
#主要
def main():
GetPosInt()
main()

这是一个简化版的代码,它使用列表存储多个学生的数据,然后在末尾显示这些详细信息,并计算班级平均数(内联注释)


COLDSPEED在我工作的时候给你发了一个问题的解决方案。如果您想看到不同的解决方案。就在这里。。。你可以为分数设置条件

def GetPosInt():
    numberOfStudents = 0.0
    classtotal = 0.0
    classavg = 0.0
    while(True):
        nam = (raw_input("Please enter a student name or '*' to finish "))
        if(nam == '*'):
            print("The average of the class is " + str(classavg))
            break
        numberOfStudents += 1.0
        scoreone = float(input("Please enter the first score for " + str(nam) + ": "))
        scoretwo = float(input("Please enter the second score for " + str(nam) + ": "))
        scorethree = float(input("Please enter the third score for " + str(nam) + ": "))
        testscores = scoreone + scoretwo + scorethree
        avg = testscores / 3.0
        print("The average score for " + str(nam) + " is " + str(avg))
        classtotal += avg
        classavg = classtotal / numberOfStudents

def main():
    GetPosInt()
main()

我发布后很快就解决了,所以我真的应该删除它。对不起,我不清楚。我只是想确定你是否记得你已经发布了那个问题作为提问者,你没有义务标记答案。而且,这个程序的解决方案是一个列表。你如何将它制作成一个列表来存储来自学生测试平均值的值当我尝试运行它时,你是如何看到的我立即被代码中的错误击中我快完成了我只需要找出如何存储来自“avg”@drewyazzie的输出值
是供您填写。它存储在
列表
数据结构中。请在这里找到更多:德鲁:我肯定ᴏʟᴅsᴘᴇᴇᴅ 了解
list
s。您只需将此答案中的
替换为类似于
int(输入(“请输入“+name+”:”)的分数)
,与您的问题类似。@martineau谢谢。
背后的全部要点不是用多余的细节来混乱代码。希望和期望是OP能够流行起来。我希望不是所有的事情都要说得那么清楚。
def GetPosInt():
    numberOfStudents = 0.0
    classtotal = 0.0
    classavg = 0.0
    while(True):
        nam = (raw_input("Please enter a student name or '*' to finish "))
        if(nam == '*'):
            print("The average of the class is " + str(classavg))
            break
        numberOfStudents += 1.0
        scoreone = float(input("Please enter the first score for " + str(nam) + ": "))
        scoretwo = float(input("Please enter the second score for " + str(nam) + ": "))
        scorethree = float(input("Please enter the third score for " + str(nam) + ": "))
        testscores = scoreone + scoretwo + scorethree
        avg = testscores / 3.0
        print("The average score for " + str(nam) + " is " + str(avg))
        classtotal += avg
        classavg = classtotal / numberOfStudents

def main():
    GetPosInt()
main()