2017-04-04 85 views
0

我不能讓我的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 
+2

當它應該結束?什麼是getNum? –

+0

'counter2 <= getNum'和'getNum()'?它不能既是一個整數也是一個函數......這段代碼可能根本不起作用。 – tdelaney

+1

什麼是getNum? –

回答

0

第4行,你的意思是?

if counter2 <= getNum() : 
    ... 
0

在except子句之後命名異常。在這種情況下,ValueError。

如果在執行try子句期間發生異常,則跳過子句 的其餘部分。然後,如果其類型與except關鍵字之後名爲 的異常相匹配,則會執行except子句,然後在try語句之後繼續執行 。

https://docs.python.org/3/tutorial/errors.html

def getGrades(): 
    while True: 
     try: 
      grades = int(input()) 
      break 
     except ValueError: 
      print("Please enter digits only") 
    return grades 
getGrades()