-1
據我所知,在我的循環中輸入了一個尾部值來結束程序。我的課程的目的是輸入一個孩子的名字,然後輸入他們的成績。當你完成一個孩子時,你輸入-1來結束程序的那部分。在你完成一個孩子後,程序應該要求你輸入另一個孩子的名字。預告片值「完成」有效,但當我輸入一個隨機的孩子名稱時,它會提示名稱未定義,並且不會運行程序的其餘部分。幫幫我?有效使用while循環
#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.")
我知道名字是無用的,但這是爲我的計算機科學類,我必須在那裏的名稱。雖然謝謝! –
我是否用raw_input語句替換了所有的輸入語句? –
@ChristianBraverman:不是'grade = input(「輸入等級:」)'ones-,或者你可以用'int(input(「輸入等級:」))'替換它們'。這個想法是,'input'(在Python 2.X中,不是Python 3)有效地讀取用戶的Python代碼(如數字或變量名),而'raw_input'正在讀取一個字符串。 –