变量未定义:但我认为它是

时间:2016-07-06 03:14:06

标签: python variables

我是编码新手,特别是在python中。大约1天前我开始使用codeacademy学习python,并且我的进展很快。在战斗部队结束后,它挑战我通过看到我能做的事来挑战自己。所以,我开始让游戏成为双人游戏。但是,在完成程序后,它告诉我第二个玩家船上的列变量没有定义。为什么呢?

以下是代码:

board1 = []
board2 = []
for i in range(5):
    board1.append(["O"] * 5)
    board2.append(["O"] * 5)
# Creates 2 boards, one for each player to view

def printboard(board):
    for row in board:
        print "  ".join(row)
# Prints one of the boards, depending on which player is meant to see it

def boardset1():
    print "Player 1, set your coordinates!"
    ship_col1 = int(raw_input("X:"))
    ship_row1 = int(raw_input("Y:"))
    if ship_col1 not in range(1,6) or ship_row1 not in range(1,6):
        print "Invalid coordinates!"
        boardset1()
    else:
        ship_col1 = abs(ship_col1 - 5)
        ship_row1 = abs(ship_row1 - 5)
        for i in range(10):
            print ""
        print "Coordinates set!"

def boardset2():
    print "Player 2, set your coordinates!"
    ship_col2 = int(raw_input("X:"))
    ship_row2 = int(raw_input("Y:"))
    if ship_col2 not in range(1,6) or ship_row2 not in range(1,6): #< Issue is here, I think
        print "Invalid coordinates!"
        boardset2()
    else:
        ship_col2 = abs(ship_col2 - 5) #< Might be here
        ship_row2 = abs(ship_row2 - 5)
        for i in range(10):
            print ""
        print "Coordinates set!"
# 2 above functions set coordinates based on player input

def play1():
    printboard(board1)
    print "Player 1: Where is the opponent's ship?"
    guess_col1 = int(raw_input("X:"))
    guess_row1 = int(raw_input("X:"))
    if guess_col1 not in range(1,6) or guess_row1 not in range(1,6):
        print "Invalid coordinates!"
        play1()
    else:
        guess_col1 = abs(guess_col1 - 5)
        guess_row1 = abs(guess_row1 - 5)
        if board1[guess_col1][guess_row1] == "X":
            print "You already guessed here!"
            play1()
        elif guess_col1 == ship_col2 and guess_row1 == ship_row2:
            win = True
            print "You have won!"
        else:
            board1[guess_col1][guess_row1] = "X"
        print "You have missed!"

def play2():
    if win == False:
        printboard(board2)
        print "Player 2: Where is the opponent's ship?"
        guess_col2 = int(raw_input("X:"))
        guess_row2 = int(raw_input("X:"))
        if guess_col2 not in range(1,6) or guess_row2 not in range(1,6):
            print "Invalid coordinates!"
            play2()
        else:
            guess_col2 = abs(guess_col2 - 5)
            guess_row2 = abs(guess_row2 - 5)
            if board2[guess_col2][guess_row2] == "X":
                print "You already guessed here!"
                play2()
            elif guess_col2 == ship_col1 and guess_row2 == ship_row1:
                win = True
                print "You have won!"
            else:
                board2[guess_col2][guess_row2] = "X"
            print "You have missed!"
# Play functions are for gameplay
win = False
boardset1()
boardset2()

for i in range(25):
    if win == False:
        play1()
        play2()
    else:
        break

在玩家1猜测后,会立即发生此错误:

    Traceback (most recent call last):
      File "python", line 97, in <module>
      File "python", line 59, in play1
    NameError: global name 'ship_col2' is not defined

欢迎任何建议或解决方案。我正在学习的越详细越好。

谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题是变量ship_col2是在函数中定义的。这意味着它只在该函数运行完成后才存在,然后被删除。为了使其在该函数和其他函数之外可用,您必须将其声明为全局变量,您可以通过在board1下的默认值和board2下面的默认值来定义它。文件。您必须定义要在多个函数中使用的所有变量。

以下是一些可能有助于您更好地理解的阅读:http://python-textbok.readthedocs.io/en/1.0/Variables_and_Scope.html