我有以下方法調用:爲什麼我的方法返回None?
NCOLS = 3
NPEGS = 4
first_guess = []
print("calling first guess method")
first_guess = firstGuess(NCOLS, NPEGS, first_guess)
print("after method call: " + str(first_guess))
firstGuess方法:
def firstGuess(NCOLS, NPEGS, first_guess):
"""Used for setting up the first guess of the game"""
print("in firstGuess method")
for c in range(1, NCOLS + 1):
if len(first_guess) == NPEGS:
print("about to return first guess: " + str(first_guess))
return first_guess
else:
first_guess.append(c)
print("out of for loop, first_guess len is " + str(len(first_guess)) + ", " + str(first_guess))
if len(first_guess) <= NPEGS: #there were less color options than pegs
firstGuess(NCOLS, NPEGS, first_guess)
這似乎是返回None
一個原因,我想不通。
這裏是我的輸出:
calling first guess method
in firstGuess method
out of for loop, first_guess len is 3, [1, 2, 3]
in firstGuess method
about to return first guess: [1, 2, 3, 1]
after method call: None
Traceback (most recent call last):
File "mastermind.py", line 323, in <module>
sys.exit(main())
File "mastermind.py", line 318, in main
playOnce()
File "mastermind.py", line 160, in playOnce
first_guess = first_guess + str(g[0][i])
TypeError: 'NoneType' object is not subscriptable
爲什麼返回None
,而不是[1, 2, 3, 1]
?
啊,我看到謝謝!愚蠢的錯誤。 – Deekor