import random
BOARD = []
for i in range(5):
BOARD.append(["O"]*5)
x = random.randint(1,5)
y = random.randint(1,5)
while True:
for i in BOARD:
print(" ".join(i))
row = int(input(": "))
column = int(input(": "))
if x == row and y == column:
print("you win")
else:
BOARD[x - 1][y - 1] = "X"
如果错过,会有像这样的输出
O O O O O
O X O O O
O O O O O
O O O O O
O O O O O
但如果我写了这个代码..它与之前类似..只有差异是第一个循环中的一个变量...它有完全不同的输出..
import random
BOARD = []
Q = ["O"]*5
for i in range(5):
BOARD.append(Q)
x = random.randint(1,5)
y = random.randint(1,5)
while True:
for i in BOARD:
print(" ".join(i))
row = int(input(": "))
column = int(input(": "))
if x == row and y == column:
print("you win")
else:
BOARD[x - 1][y - 1] = "X"
但是有这样的输出..它对我来说没有意义
O x O O O
O X O O O
O x O O O
O x O O O
O x O O O
在这个网站上有谁能解释这个???没有人能帮忙
答案 0 :(得分:1)
这必须解决问题:
import random
BOARD = []
Q = ["O"]*5
for i in range(5):
BOARD.append(list(Q))
x = random.randint(1,5)
y = random.randint(1,5)
while True:
for i in BOARD:
print(" ".join(i))
row = int(input(": "))
column = int(input(": "))
if x == row and y == column:
print("you win")
else:
BOARD[x - 1][y - 1] = "X"
您必须复制清单Q:BOARD.append(list(Q))