【发布时间】:2011-02-04 01:33:24
【问题描述】:
我无法让我的列表在我的代码中返回。它没有返回列表,而是继续返回 None,但是如果我在 elif 语句中将返回替换为 print,它会很好地打印列表。我该如何修复这个?
def makeChange2(amount, coinDenomination, listofcoins = None):
#makes a list of coins from an amount given by using a greedy algorithm
coinDenomination.sort()
#reverse the list to make the largest position 0 at all times
coinDenomination.reverse()
#assigns list
if listofcoins is None:
listofcoins = []
if amount >= coinDenomination[0]:
listofcoins = listofcoins + [coinDenomination[0]]
makeChange2((amount - coinDenomination[0]), coinDenomination, listofcoins)
elif amount == 0:
return listofcoins
else:
makeChange2(amount, coinDenomination[1:], listofcoins)
【问题讨论】:
-
Nate, Mark Rushakoff 回答了你的问题;你应该接受他的回答。您可以通过单击答案的投票计数下的复选标记 (✓) 来做到这一点。