我不能讓我的while循環inf getClasses函數在它應該結束時結束。 getNum和counter都被設置爲整數。我也試圖做while True
和if語句來看看這是否會有所幫助,但這只是給我一個無限循環。我知道這是一個簡單的問題,但我無法弄清楚我做錯了什麼。請幫忙。蟒蛇中無處不在while循環
def getNum():
while True:
try:
num = int(raw_input("How many classes do you have:\n")) #Asks the user for the number of classes he/she has.
break
except:
print("Input must be a number")
return num
def getGrades(): #Gets the grades from the user
counter2 = 1
global grades
if counter2 <= getNum: #If the 2nd counter is less than or equa to the number of classes...
while True:
try:
grades = int(raw_input("What is the grade for class %s:\n" %counter2)) #Asks the user what their grades are for 'counter' class.
counter2 += 1 #...increase the 2nd counter by 1
break
except:
print("Input must be a number")
return grades
def getClasses(): #Gets the user's classes
counter = 1
getNum()
while counter <= getNum: #If the counter is less than or equal to the number of classes...
classes = str(raw_input("Class %s is what:\n" %counter)) #Asks the user what their 'counter' class is.
subjects[classes] = getGrades()
counter += 1 #...increase the counter by 1
return subjects
當它應該結束?什麼是getNum? –
'counter2 <= getNum'和'getNum()'?它不能既是一個整數也是一個函數......這段代碼可能根本不起作用。 – tdelaney
什麼是getNum? –