gpt4 book ai didi

python - 我的战舰代码缺乏定义的问题

转载 作者:行者123 更新时间:2023-12-03 09:08:56 25 4
gpt4 key购买 nike

需要一些我正在编写的战舰代码方面的帮助,但遇到了一些麻烦。我不知道怎么了。我尝试使用return语句,但是没有用,这个错误不断 pop :

#NameError: name 'air_carier_X' is not defined on line 230

这是我的代码:
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(air_carier, air_carier_X, air_carier_Y):
#makes sure 2 things are entered
length = len(air_carier)
if length != 2:
while length != 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_X = air_carier[0] # make sure thats a letter=========================================================
#makes sure x axis value is a number
while True:
try:
air_carier_Y = 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_Y = int(air_carier[1])
continue
else:
break
#makes sure number entered is a chosable number
if air_carier_X not in Row_letters:
while air_carier_X not in Row_letters:
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
if air_carier_Y not in Row_numbers:
while air_carier_Y not in Row_numbers:
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_X = Alph2num[air_carier_X]
if air_carier_X > 6:
while air_carier_X > 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 askcheck(air_carier_X, air_carier_Y):
air_carier = input("Where would you like your air carrier?:")
#makes sure 2 things are entered
length = len(air_carier)
if length != 2:
while length != 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_X = air_carier[0] # make sure thats a letter=========================================================
#makes sure x axis value is a number
while True:
try:
air_carier_Y = 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_Y = int(air_carier[1])
continue
else:
precheck(air_carier, air_carier_X, air_carier_Y)
break
#makes sure number entered is a chosable number
if air_carier_X not in Row_letters:
while air_carier_X not in Row_letters:
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(air_carier, air_carier_X, air_carier_Y)
break
#makes sure number entered is a chosable letter
if air_carier_Y not in Row_numbers:
while air_carier_Y not in Row_numbers:
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(air_carier, air_carier_X, air_carier_Y)
break

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

def place_air_carier_vert(air_carier_X, air_carier_Y):
print " "
askcheck(air_carier_X, air_carier_Y)
print air_carier_X
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

print air_carier_Y
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_carier_X, air_carier_Y)

最佳答案

在最后一行代码中:

place_air_carier_vert(air_carier_X, air_carier_Y)

从未在全局范围内定义 air_carier_Xair_carier_Y。可以将它们替换为要传递给 place_air_carier_vert的内容,或者将它们初始化为所需的内容。

关于python - 我的战舰代码缺乏定义的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53218810/

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