2016-12-26 4 views
-2

MIT OpenCourseware Sc 6.00で質問を試みました。 質問は6、9、20パックのマクナゲットと合計数の組み合わせを見つける方法です。このコードを実行するときTypeError: 'NoneType'オブジェクトはMIT Opencourseの割り当てに対して反復できません

def Mac(): 
    totalnumber = int (input('Enter total number of McNuggets')) 
    num20,num6,num9= solve2(totalnumber) 

def solve2(numtotal): #define a new function to find and print multiple answers 
    solutionfound = False # set up a variable to check if the solution is found 
    for num20 in range (0,numtotal//20 + 1): 
     for num9 in range (0,numtotal//9 +1): 
      num6 = (numtotal-num20*20-num9*9)//6 
      if num6 > 0: 
       checknum = num20*20+num9*9+num6*6 
       if checknum == numtotal: 
        print ('The number of 6-pack is', num6) 
        print ('The number of 9-pack is', num9) 
        print ('The number of 20-pack is', num20) 
        solutionfound = True # change the initial variable to True 
    if not solutionfound: 
     print ('There is no solution') 

しかし、それは常に表示: は現在、私のコードがある

TypeError: 'NoneType' object is not iterable

+0

コード内のどの行が例外を発生させていますか? – elethan

+1

'solve2'は' None'を返します、pythonはそれを解凍しようとしています。 – TheClonerx

+0

@Nurzhan編集時にPythonコード内の空白を変更することはお勧めしません。 – MYGz

答えて

1

機能solve2()は、任意の値を返していないので、その戻り値はNoneであり、あなたがしようとしていますそれを繰り返すにはnum20,num6,num9= solve2(totalnumber)を実行します。したがって、コードのこの部分はTypeError: 'NoneType' object is not iterableとなります。

コードを見ると値を返す場所を決めることができないので、値を返す場所はどこでもreturnを使用してください。

1

あなたはこれを試すことができます。

def Mac(): 
    totalnumber = int (input('Enter total number of McNuggets: ')) 
    num20,num6,num9 = solve2(totalnumber) 

def solve2(numtotal): #define a new function to find and print multiple answers 
    solutionfound = False # set up a variable to check if the solution is found 
    for num20 in range (0,numtotal//20 + 1): 
     for num9 in range (0,numtotal//9 +1): 
      num6 = (numtotal-num20*20-num9*9)//6 
      if num6 > 0: 
       checknum = num20*20+num9*9+num6*6 
       if checknum == numtotal: 
        print ('The number of 6-pack is', num6) 
        print ('The number of 9-pack is', num9) 
        print ('The number of 20-pack is', num20) 
        solutionfound = True # change the initial variable to True 
        return (num6, num9, num20) 
    if not solutionfound: 
     print ('There is no solution') 
     return (None, None, None) 

Mac() 

それは正しくあなたの方法のsolve2から値を返す必要が指摘されたとおり。

関連する問題