我一直收到错误消息“没有足够的值来解包(预期2,得到1)
下面是代码:
def gap(): # generates a bunch of blank lines in the console to hide previous moves
for i in range(30):
print("")
def grid(): # The Grid that the game is in
t = [] # the name of the actual grid list
grid_size = 10 # self explanitory
for i in range(grid_size): # this is called a for loop
row = []
for q in range(grid_size):
row.append(" ")
t.append(row)
def move_player_1():
while True:
gap()
for i in range(len(t)):# this is called a for loop
print(t[i])
print('')
#try:
print("Choose what Ship to place")
print('')
print('')
print("Write '2' for two long ship")
print('')
print("Write '3' for three long ship")
print('')
print("Write '4' for four long ship")
print('')
print("Write '5' for five long ship")
print('')
ship_length = int(input("Your Ship choice:"))
placement_chords = input("player 1 (place ships) eg: 1,7 : ")
row, col = placement_chords.split(",") # divides "move" into separate integers for row and column
print(row)
print(col)
counter = 0
for i in range(ship_length):
t[row][counter],[col] = "0"
print(t)
counter += 1
row, col = placement_chords.split(",")
row -= 1 # this just subtracts one to fix things, if a player selected the coordanates "1,1" it would actully happen on 2,2 because lists are weird
col -=1
if t[row][col] == " ": # hit detection
t[row][col] = "x"
else:
print("HIT!")
gap()
for i in range(len(t)):# this is called a for loop and it is printing the board
print(t[i])
return t
#except ValueError:
#print("You have done it wrong")
#time.sleep(2)
move_player_1()#this is a definition
grid()
我期待相关的位置被填充为0,以充当战舰的舰船
1条答案
按热度按时间mwg9r5ms1#
这是有问题的一行
逗号表示您希望进行两次分配,一次分配给
t[row][counter]
,另一次分配给[col]
。这没有意义,也不是您想要做的。下面应该是:但是,为了确保从
col
开始填充,您应该将counter
初始化为col
,而不是0,因此:如果用户输入的列离网格的右边太远,这仍然会给予问题,在这种情况下,循环会将
counter
增加到一个超出范围的值,因此您需要添加一个检查,检查输入在这个意义上是否有效。说到输入的验证,你的代码不会把输入转换成数字,所以
row
和col
是字符串,但它们应该是数字,如果你需要数字输入,总是把输入转换成int
,你可以用map
把这个转换应用到所有的输入值上:您不需要执行两次split,因此删除第二次spit调用--您已经有了
row
和col
的值......您的代码中还有很多需要改进的地方,但这超出了您当前问题的范围。