战舰参考错误

时间:2014-07-29 10:33:18

标签: python

我的未完成战舰游戏有问题。 我做了3天或4天的python而没有计算更大的停顿时间(我在开始学习后2天进行了2周的旅行)。 这是我的第一语言。 当我尝试运行它时,它说:

Welcome to Battleships!!!
Let's Begin!
Traceback (most recent call last):
  File "/home/anna/Dokumenty/battleship.py", line 49, in <module>
    setup_ships()
  File "/home/anna/Dokumenty/battleship.py", line 12, in setup_ships
    ships_pos()
  File "/home/anna/Dokumenty/battleship.py", line 39, in ships_pos
    if direction == 1 and pos_col + 4 > 9:
UnboundLocalError: local variable 'pos_col' referenced before assignment

我无法弄清楚为什么会这样说,因为我只是在参考之前设置它 有代码:

print "Welcome to Battleships!!!"
board = []
import os
from random import randint
for x in range(10):
    board.append(["O"] * 10)
def print_board(board):
    for row in board:
        print " | ".join(row)
ships = [[2, 4, 1], [3, 4, 2], [4, 2, 3], [5, 1, 4]]
def setup_ships():
    ships_pos()
    for ship in ships:
        for n in range(ship[1]):
            if direction == 0:                         
                for ship_types_row in ships_row:
                    for ship_row in ship_types_row:
                        for n in range(ship[0]):                                                    
                             coordinates_row = n + 1 + ship_row[0]
                             ship_row.append(coordinates_row)      
            if direction == 1:                
                for ship_types_col in ships_col:
                    for ship_col in ship_types_col:
                        for n in range(ship[0]):                    
                            coordinates_col = n + 1 + ship_col[0]
                            ship_col.append(coordinates_col)
def ships_pos(): #makes starting position of every ship
    direction = randint(0,2)  
    for ship_types_row in ships_row:
        for ship_row in ship_types_row:
            pos_row = randint(0,10) - 1
            if direction == 0 and pos_row + 4 > 9:
                pos_row = randint(0,6) - 1
                ship_row.append(pos_row)          
            else:
                ship_row.append(pos_row)
    for ship_types_col in ships_col:
        for ship_col in ship_types_col:
            if direction == 1 and pos_col + 4 > 9:
                pos_col = randint(0,6) - 1
                ship_col.append(pos_col)          
            else:
                ship_col.append(pos_col)

print "Let's Begin!"
while True:
    ships_row = [[[], [], [], []], [[], [], [], []], [[], []], [[]]]
    ships_col = [[[], [], [], []], [[], [], [], []], [[], []], [[]]]
    setup_ships()
    turn = 1    
    print "Turn", turn    
    print_board(board)
    guess_row = int(raw_input("Guess Row: ")) - 1
    guess_col = int(raw_input("Guess Colon: ")) - 1

    while True: 
        for ship_type in ships_row:
            for ship_row in ship_type:
                for x in ship_row:
                    if x == guess_row:
                        ship_type_index = ships_row.index(ship_type)
                        ship_row_index = ship_type.index(ship_row)
                        x_index = ship_row.index(x)
                        if guess_colon == ships_col[ship_type_index][ship_row_index][x_index]:
                            print "A ship was hit!!!"
        if (guess_row < 0 or guess_row > 9) or (guess_col < 0 or guess_col > 9):
            print "Oops, that's not even in the ocean."
            break
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
            break
        else:
            print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
            break
    print_board(board)
    turn += 1

1 个答案:

答案 0 :(得分:0)

这是因为当时没有定义pos_col。

也许你错过了

pos_col = randint(0,10) - 1 
第38行