2017-04-04 16 views
0

私のwhileループinfを取得できません。getClasses関数は、終了時に終了することができません。 getNumとcounterの両方が整数として設定されます。私もwhile Trueとif文を使って、それが助けになるかどうかを調べようとしましたが、無限ループになります。私はこれが単純な問題であることを知っていますが、私が間違っていることが何であるかは分かりません。助けてください。Unending While Pythonのループ

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 exceptionsの後に例外名を付けます。この場合は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()