- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对我的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:
air_carier = input("Where would you like your air carier?:")
的行将解决此问题。
关于python - 试图对我的Python Battleship游戏进行白痴验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53183597/
我是 C++ 的新手,我在使用这段代码时遇到了问题: string output_date(int day, int month, int year){ string date; if
所以我这样做了 tar cvzf test.zip FP 为了创建目录 FP 的 zip 但是,它会列出 zip 中的目录 FP/ FP/php/ FP/php/pdf/ FP/php/docs/ F
我正在尝试在 Swift、Xcode 7.3(所以是 Swift 2.2)中创建一个通用类,但我似乎无法让它通过编译器: protocol Struct1Protocol { } struct Str
我的测试用例是这样的: class FooTest extends PHPUnit_Framework_TestCase { /** @covers MyClass::bar */ f
我正在尝试将brew install wine作为使electron-builder工作的一步。但是我所能得到的只是以下响应: ==> Installing dependencies for wine
我这样做: string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}}; int b = 0; for(int i = 0; i <=
我正在尝试使用 SetWindowsHookEx 键盘 Hook Notepad.exe。 如您所见,工作线程正在将其 ASCII 代码(即 wParam)发送到指定的服务器。 UINT WINAPI
我正在尝试将 ListView 实现到我的 Fragment 中,但无论我尝试什么,我都会得到一个 NullPointerException。我检查对象是否为 null 并记录是否为 null,看起来
我尝试在一行中对齐两个 div。使用 float left 属性,一切顺利。但是当我在 div 中使用图像时,它开始产生问题。 所以这是我的示例代码:- Some headi
我目前正在使用此代码来获取图像的灰度图像表示并以 (512, 370, 1) 的格式表示它大批。 img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
总结 我正在创建一个简单的应用程序,它允许用户选择一个包含顶级窗口的进程。用户首先键入 native DLL(而非托管 DLL)的路径。然后用户键入将在 Hook 过程中调用的方法的名称。该方法不得返
我是一名优秀的程序员,十分优秀!