尝试在麻省理工学院开放式课件sc6.00上提问。 问题是如何找到6,9,20包的麦金块组合,并给出总数量。 目前我的代码是:
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
函数
solve2()
不返回任何值,因此它的返回值是None
,您正试图通过执行num20,num6,num9= solve2(totalnumber)
来迭代它。所以这部分代码会引发TypeError: 'NoneType' object is not iterable
。在查看代码,我无法决定从何处返回值,因此无论您想返回值,只要使用
return
。在你可以试试这个:
正确地指出,您需要从方法solve2返回值。在
相关问题 更多 >
编程相关推荐