gpt4 book ai didi

python - 试图对我的Python Battleship游戏进行白痴验证

转载 作者:行者123 更新时间:2023-12-03 08:22:13 28 4
gpt4 key购买 nike

我正在尝试对我的Python Battleship游戏进行白痴验证,但是当我运行它时,此错误不断 pop :

UnboundLocalError: local variable 'air_carier' referenced before assignment on line 127



我尝试不使其成为函数并更改代码的位置,但是我没有任何运气。有人可以帮忙吗?我什至不知道这个错误是什么意思。
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()

最佳答案

您正在尝试在此行分配之前使用air_carrier:

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

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

关于python - 试图对我的Python Battleship游戏进行白痴验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53183597/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com