试图白痴证明我的python战舰

时间:2018-11-07 04:31:41

标签: python error-handling while-loop

试图愚弄我的python战舰,但是当我运行它时,此错误不断弹出:

UnboundLocalError:在第127行赋值之前引用了本地变量“ air_carier”

我试图不使其功能化,并更改代码在代码中的位置,但是我没有运气可以帮助别人,我什至不知道这种错误是什么意思。

row_dict1 = {
    "row_A" : " A  |   |   |   |   |   |   |   |   |   |   |",
    "row_B" : " B  |   |   |   |   |   |   |   |   |   |   |",
    "row_C" : " C  |   |   |   |   |   |   |   |   |   |   |",
    "row_D" : " D  |   |   |   |   |   |   |   |   |   |   |",
    "row_E" : " E  |   |   |   |   |   |   |   |   |   |   |",
    "row_F" : " F  |   |   |   |   |   |   |   |   |   |   |",
    "row_G" : " G  |   |   |   |   |   |   |   |   |   |   |",
    "row_H" : " H  |   |   |   |   |   |   |   |   |   |   |",
    "row_I" : " I  |   |   |   |   |   |   |   |   |   |   |",
    "row_J" : " J  |   |   |   |   |   |   |   |   |   |   |"
}

X_row_lines = {
    "A" : "  |   |   |   |   |   |   |   |   |   |   |",
    "B" : "  |   |   |   |   |   |   |   |   |   |   |",
    "C" : "  |   |   |   |   |   |   |   |   |   |   |",
    "D" : "  |   |   |   |   |   |   |   |   |   |   |",
    "E" : "  |   |   |   |   |   |   |   |   |   |   |",
    "F" : "  |   |   |   |   |   |   |   |   |   |   |",
    "G" : "  |   |   |   |   |   |   |   |   |   |   |",
    "H" : "  |   |   |   |   |   |   |   |   |   |   |",
    "I" : "  |   |   |   |   |   |   |   |   |   |   |",
    "J" : "  |   |   |   |   |   |   |   |   |   |   |",
}


row_sub = {"Xaxis":"row_"}

Num2alph = {0:"A", 1:"A", 2:"B", 3:"C", 4:"D", 5:"E", 6:"F", 7:"G", 8:"H", 9:"I", 10:"J"
, 11:"K", 12:"L", 13:"M", 14:"N", 15:"O", 16:"P", 17:"Q", 18:"R", 19:"S"
, 20:"T", 21:"U", 22:"V", 23:"W", 24:"X", 25:"Y", 26:"Z"
}

Alph2num = {"A":1, "B":2, "C":3, "D":4, "E":5, "F":6, "G":7, "H":8, "I":9
, "J":10, "K":11, "L":12, "M":13
, "N":14, "O":15, "P":16, "Q":17, "R":18, "S":19, "T":20, "U":21, "V":22
, "W":23, "X":24, "Y":25, "Z":26, 
}

Row_letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
Row_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def print_board():
    print "      1   2   3   4   5   6   7   8   9  10"
    print "    ________________________________________"
    print row_dict1["row_A"]
    print "    |---------------------------------------|"
    print row_dict1["row_B"]
    print "    |---------------------------------------|"
    print row_dict1["row_C"]
    print "    |---------------------------------------|"
    print row_dict1["row_D"]
    print "    |---------------------------------------|"
    print row_dict1["row_E"]
    print "    |---------------------------------------|"
    print row_dict1["row_F"]
    print "    |---------------------------------------|"
    print row_dict1["row_G"]
    print "    |---------------------------------------|"
    print row_dict1["row_H"]
    print "    |---------------------------------------|"
    print row_dict1["row_I"]
    print "    |---------------------------------------|"
    print row_dict1["row_J"]
    print "    |_______________________________________|"
    print " "
print_board()



def precheck():
    #makes sure 2 things are entered
    while str(len(air_carier)) != 2:
        try:
            print "please print in format (row letter)(row number)"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            break
    air_carier = air_carier.upper()
    air_carier_Y = air_carier[0]
    #makes sure x axis value is a number
    while True:
        try:
            air_carier_X = int(air_carier[1])
        except:
            print "please have your y-axis code as a number"
            air_carier = input("Where would you like your air carier?:")
            air_carier_X = int(air_carier[1])
            continue
        else:
            break
    #makes sure number entered is a chosable number
    while air_carier_X not in Row_numbers:
        try:
            print "please choose a number that is in the grid"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            break
    #makes sure number entered is a chosable letter
    while air_carier_Y not in Row_letters:
        try:
            print "please choose a letter that is in the grid"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            break

    #makes sure ship will fit on board
    air_carier_Y = Alph2num[air_carier_Y]
    while air_carier_Y > 6:
        try:
            print "please chose a y coordinate above G"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            break
def check():
    #makes sure 2 things are entered
    while str(len(air_carier)) != 2:
        try:
            print "please print in format (row letter)(row number)"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            precheck()
            break
    air_carier = air_carier.upper()
    air_carier_Y = air_carier[0]
    #makes sure x axis value is a number
    while True:
        try:
            air_carier_X = int(air_carier[1])
        except:
            print "please have your y-axis code as a number"
            air_carier = input("Where would you like your air carier?:")
            air_carier_X = int(air_carier[1])
            continue
        else:
            precheck()
            break
    #makes sure number entered is a chosable number
    while air_carier_X not in Row_numbers:
        try:
            print "please choose a number that is in the grid"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            precheck()
            break
    #makes sure number entered is a chosable letter
    while air_carier_Y not in Row_letters:
        try:
            print "please choose a letter that is in the grid"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            precheck()
            break

    #makes sure ship will fit on board
    air_carier_Y = Alph2num[air_carier_Y]
    while air_carier_Y > 6:
        try:
            print "please chose a y coordinate above G"
            air_carier = input("Where would you like your air carier?:")
        except:
            continue
        else:
            precheck()
            break

def place_air_carier_vert():
    print " "
    air_carier = input("Where would you like your air carier?:")
    check()
    X_coordinate = row_sub["Xaxis"] + air_carier_X#turns into row form(row_B)
    X_row = row_dict1[X_coordinate]#gets the grid value based on row

    air_carier_Y = 3 + (4*air_carier_Y)#finds how far in the grid row we need to go in to center the ship based on the number given

    X = air_carier_Y - 1#buffers the ship location by one in order to keep the ship centered
    Y = air_carier_Y + 1#see above comment

    X_row = X_row[:X] + "O " + X_row[Y:]#inserts O in representation of a ship into grid
    X_row_lines[air_carier_X] = X_row[2:]#saved the grid value in order to prevent the second time called to erase the first
    row_dict1[X_coordinate] = X_row


    for i in range(4):
        air_carier_X = Alph2num[air_carier_X]
        air_carier_X = air_carier_X + 1
        air_carier_X = Num2alph[air_carier_X]

        X_coordinate = row_sub["Xaxis"] + air_carier_X
        X_row = " " + air_carier_X + X_row_lines[air_carier_X]
        X_row = X_row[:X] + "O " + X_row[Y:]
        X_row_lines[air_carier_X] = X_row[2:]
        row_dict1[X_coordinate] = X_row
    print " "
    print " "
    print " "
    print_board()


place_air_carier_vert()

3 个答案:

答案 0 :(得分:1)

您没有提到air_carier是什么,所以python会给您这个错误。您必须在函数之外提及它...

答案 1 :(得分:0)

您正在尝试使用air_carrier,然后再在此行中对其进行分配:

while str(len(air_carier)) != 2:

在while循环开始之前添加诸如air_carier = input("Where would you like your air carier?:")之类的行将解决此问题。

答案 2 :(得分:0)

错误通常是因为您在函数中使用错误的变量的方式。一个例子:

x = 4        # Assign x in the global scope
def adder():
    print(x) # Use x. As x is not assigned in the local scope
             # the golbal x will be used
    x = 5    # Assign fails as the fonction may not assign
             # names in the global scope

因此,寻找使用在函数外部分配的变量的函数,并在尝试为其分配变量的地方会发现问题。