有效使用while循环

时间:2013-01-22 00:38:24

标签: python while-loop nested-loops

我知道在我的循环中输入了预告片值以结束程序。我的计划的目的是输入孩子的名字,然后输入他们的成绩。完成一个孩子后,输入-1即可结束该程序的那一部分。完成一个孩子后,该程序应该要求您输入另一个孩子的名字。预告片值“Done”有效,但是当我输入一个随机的孩子名字时,它会询问它是否定义了名称,并且它不会运行程序的其余部分。救命?

#By: Christian Braverman 01/02/13

#Variables

TotalNum_A = 0
TotalNum_B = 0
TotalNum_C = 0
TotalNum_D = 0
TotalNum_F = 0
studentname = 0
Done = 0
grade = 0

#Loop
studentname = input("Enter student name ")
while studentname != Done :                    
    studentname = input ("Enter student name ")
    grade = input("Enter a Grade ")
    while grade != -1:
        grade = input("Enter a Grade: ")

        if grade >=90 :
            print "A!"
            TotalNum_A = TotalNum_A + 1


        if grade >=80 and grade <90:
            print "B!"
            TotalNum_B = TotalNum_B + 1


        if grade >=70 and grade <80:
            print "C"
            TotalNum_C = TotalNum_C + 1


        if grade >=60 and grade <70:
            print "D"
            TotalNum_D = TotalNum_D + 1


        if grade <=59:
            print "F :("
            TotalNum_F = TotalNum_F + 1
    grade = input("Enter a Grade: ")

#Printing Totals

print ("You received " + str(TotalNum_A) + " A's.")

print ("You recieved " + str(TotalNum_B) + " B's.")

print ("You recieved " + str(TotalNum_C) + " C's.")

print ("You recieved " + str(TotalNum_D) + " D's.")

print ("You recieved " + str(TotalNum_F) + " F's.")

1 个答案:

答案 0 :(得分:0)

你应该使用raw_input而不是input - 这样,它将需要一个字符串,而不是要求输入是一个已经存在的变量。然后替换

while studentname != Done:

while studentname != "Done":                    

请注意,到目前为止,没有理由要求学生的姓名,因为截至目前,您的课程对他们没有任何帮助。