迭代的全局变量不会触发==条件

时间:2017-05-06 04:03:35

标签: python scope global

我尝试将turn = 0放在play函数中 我已经在Stackoverflow上阅读了几个类似的问题,并尝试将global turn放在我的函数内外。这些并没有抛出错误,但也没有导致转弯限制。为什么?关于如何解决这个问题的任何想法?

我的直觉是因为我已将变量turn设置为global我会继续引用turn=0来获取它的价值,因为这样做&{ #39; s设置在函数外部,但由于它会在你轮流时运行迭代数字,所以我错了。

这是我的代码:

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Incredibly Simple Battleship!"
print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row #erase after testing
print ship_col

global turn

def play(guess_row,guess_col):
  global turn
  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
  else:
    if turn == 4:
        print "Game Over"
    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that one already."
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
        print_board(board)
        print "Guess again:"
    print "Turn ",turn,":"
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    turn += 1
    play(guess_row,guess_col)  


turn=0
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))      
play(guess_row,guess_col)

这是我没有使用global时的原始错误:

Traceback (most recent call last):
 File "python", line 51, in <module>
 File "python", line 30, in play
UnboundLocalError: local variable 'turn' referenced before assignment

注意我想接受不使用global的建议,但还没有找到替代方案。

1 个答案:

答案 0 :(得分:1)

这是一个通过递归play()函数的额外变量跟踪转弯的版本。注意我还为你添加了一个回报,以便游戏结束游戏结束。 :)

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

print "Incredibly Simple Battleship!"
print_board(board)

def random_row(board):
    return randint(0, len(board) - 1)

def random_col(board):
    return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row #erase after testing
print ship_col


def play(guess_row,guess_col, turn):
  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
  else:
    if turn == 4:
        print "Game Over"
        return
    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that one already."
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"
        print_board(board)
        print "Guess again:"
    print "Turn ",turn,":"
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    play(guess_row,guess_col, turn + 1)


guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
play(guess_row,guess_col, 0)