我有以下方法调用:
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))
第一种猜测方法:
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]
?