提问人:Oran Kelly 提问时间:1/30/2023 最后编辑:NishantOran Kelly 更新时间:1/30/2023 访问量:42
为什么我的代码没有让我在这个循环中输入?它不读取它 [复制]
Why is my code not getting me to input in this loop? It doesn't read it [duplicate]
问:
所以我正在尝试制作一个井字游戏,并且我做得很好,但是在创建循环定义时,我要求输入,但是当我运行它时,我从未被提示输入我的动作
board = [["1", "2", "3"], ["4", "X", "6"], ["7", "8", "9"]]
def display_board(board):
print("+-------+-------+-------+")
print("| | | |")
print("| " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " |")
print("| | | |")
print("+-------+-------+-------+")
print("| | | |")
print("| " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " |")
print("| | | |")
print("+-------+-------+-------+")
print("| | | |")
print("| " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " |")
print("| | | |")
print("+-------+-------+-------+")
def enter_move(board):
while True:
move = int(input("Please pick a number within the range of squares (1-9): "))
if move < 1 or move > 9:
print("Error please pick a number 1 through 9: ")
continue
elif move not in [0] and move not in [1] and move not in [2]:
print("Sorry please try another move that square is already taken!")
continue
elif move == 1:
board[0][0] == "O"
elif move == 2:
board[0][1] == "O"
elif move == 3:
board[0][2] == "O"
elif move == 4:
board[1][0] == "O"
elif move == 6:
board[1][2] == "O"
elif move == 7:
board[2][0] == "O"
elif move == 8:
board[2][1] == "O"
elif move == 9:
board[2][2] == "O"
break
我希望我在单击运行时输入一个整数,但什么也没发生
答:
0赞
Adarsh Gupta
1/30/2023
#1
您尚未在代码中调用该函数。在代码末尾写下这个!enter_move()
if __name__ == "__main__":
enter_move(board)
或者你可以直接使用循环,而没有调用函数,比如:while
enter_move()
board = [["1", "2", "3"], ["4", "X", "6"], ["7", "8", "9"]]
def display_board(board):
print("+-------+-------+-------+")
print("| | | |")
print("| " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " |")
print("| | | |")
print("+-------+-------+-------+")
print("| | | |")
print("| " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " |")
print("| | | |")
print("+-------+-------+-------+")
print("| | | |")
print("| " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " |")
print("| | | |")
print("+-------+-------+-------+")
while True:
move = int(input("Please pick a number within the range of squares (1-9): "))
if move < 1 or move > 9:
print("Error please pick a number 1 through 9: ")
continue
elif move not in [0] and move not in [1] and move not in [2]:
print("Sorry please try another move that square is already taken!")
continue
elif move == 1:
board[0][0] == "O"
elif move == 2:
board[0][1] == "O"
elif move == 3:
board[0][2] == "O"
elif move == 4:
board[1][0] == "O"
elif move == 6:
board[1][2] == "O"
elif move == 7:
board[2][0] == "O"
elif move == 8:
board[2][1] == "O"
elif move == 9:
board[2][2] == "O"
break
评论
0赞
Yash Mehta
1/30/2023
enter_move
还需要一个参数..!
0赞
Oran Kelly
1/30/2023
哎呀,是的,傻脑子,谢谢!
0赞
Yash Mehta
1/30/2023
你运行第一个代码,它工作吗..!我不这么认为,因为您没有在 ..@Apne杜尼亚enter_move()
0赞
Adarsh Gupta
1/30/2023
@YashMehta没有必要,因为它已经在接受输入了!
0赞
Yash Mehta
1/30/2023
不明白你想说什么,你能不能更简短一点..!@ApneDuniya
评论
enter_move()
enter_move()
display_board()