# 为什么我的方法返回None？Why is my method returning 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)
``````

``````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
``````

## 3 个回复3

### ===============>>#1 票数：3 已采纳

``````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
return firstGuess(NCOLS, NPEGS, first_guess)
``````

### ===============>>#2 票数：0

``````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
return firstGuess(NCOLS, NPEGS, first_guess)
return first_guess
``````

### ===============>>#3 票数：0

``````if len(first_guess) <= NPEGS: #there were less color options than pegs
return firstGuess(NCOLS, NPEGS, first_guess)
else:
# what do you do here?  return something
return first_guess
``````