【发布时间】:2020-04-11 07:33:18
【问题描述】:
我正处于用 python 编写一个简单的井字游戏的早期阶段。我已经定义了 3 个函数,一个是初始化游戏,一个是绘制棋盘,一个是询问玩家是 X 还是 O。据我所知,我觉得我的函数是按顺序和正确的顺序请求的,但我无法让程序移过第一个输入部分。任何帮助都会很棒。
def start():
print("Do you want to play Tic Tac Toe? Type yes or no.")
choice = input()
while choice == ('yes','no'):
if choice.lower == ('yes'):
player_choice()
elif choice.lower == ('no'):
print("goodbye")
quit()
else:
print("That was not a valid response. Type yes or no.")
start()
def drawsmall():
a = (' ___' * 3 )
b = ' '.join('||||')
print('\n'.join((a, b, a, b, a, b, a, )))
def player_choice():
print("Player one it's time to choose, X or O")
select= input()
if select.lower ==("x","o"):
print("Let the game begin.")
drawsmall()
elif select.lower != ('x','o'):
print("Please choose either X or O.")
else:
print("Come back when you're done messing around.")
quit()
【问题讨论】:
-
你能用文字解释一下你期望看到的事件的顺序吗?
-
是的,我很抱歉。基本上,在我看来,流程是这样的。 def start(),初始化游戏并要求玩家输入对应于是或否的输入。如果是,则应该将其传递给 player_choice,它会询问玩家一他们想要成为 X 还是 O。玩家选择应该通过,玩家一选择 X 还是 O 来绘制小,它应该绘制实际的棋盘。
-
我认为第一个问题(没有所有代码很难知道完整的流程)在这里:'if select.lower ==("x","o")',应该是'if select.lower in ("x", "o")';和你的 elif 一样,它应该只是 'else:' (选择可能等于或不等于“x”或“o”。它不能是任何其他东西)。
-
抱歉,之前没看到。这里也是一样的:while 选择 == ('yes','no')。应该是'while selection not in ('yes', 'no'):'
标签: python tic-tac-toe