[已编辑] 为简单起见,我将在此处包含整个文件:
choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
Is_Current_One = True
won = False
wrongEntryPlayer1 = False
wrongEntryPlayer2 = False
while not won:
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
print('\n')
print('|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
print('----------')
print('|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
print('----------')
print('|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
# above code is to print board layouts
if Is_Current_One:
print("Player 1's turn.\nChoose a number to put an X there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Player 2's turn.\nChoose a number to put an O there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
if Is_Current_One:
if choices[choice - 1] == int:
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
else:
if choices[choice - 1] == int:
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
# code to toggle between True and False
Is_Current_One = not Is_Current_One
for pos_x in range(0, 3):
pos_y = pos_x * 3
# for row condition:
if (choices[pos_y] == choices[(pos_y + 1)]) and (
choices[pos_y] == choices[(pos_y + 2)]):
won = True
# column condition:
if (choices[pos_x] == choices[(pos_x + 3)]) and (
choices[pos_x] == choices[(pos_x + 6)]):
won = True
if ((choices[0] == choices[4] and choices[0] == choices[8])
or (choices[2] == choices[4] and choices[4] == choices[6])):
won = True
print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
如果你真的阅读了代码,你会发现我正在制作一个井字游戏。
我已经声明了两个布尔变量:wrongEntryPlayer1
和 wrongEntryPlayer2
,它们最初默认设置为 False
,因为游戏刚刚开始。
玩这个游戏时,玩家需要输入一个整数,在游戏板上显示,在那里放一个X
或O
。
如果玩家不输入一个整数,程序就会崩溃,我不希望那样。相反,当他们没有输入整数时,我使用了 try
语句并打印了一条用户友好的消息。
发生这种情况时,我不希望 Python 重新打印游戏板,因此当玩家输入错误值时,我将相应的 wrongEntryPlayer 变量设置为 True。这样,如果任何一个玩家输入了错误的值,我将完全跳过打印游戏板。
但是,当我尝试运行它并在第一回合输入错误的值时,然后输入正确的值,游戏板仍然不显示。即使轮到玩家 2,游戏板仍然没有打印出来。
当玩家输入正确的值(整数)时,游戏板仍然不显示。
另外,当玩家输入已经被其他玩家占用的值时,程序应该提示玩家再次输入。相反,它只是跳过玩家的回合。
我是一个python初学者,所以有些代码可能看起来很傻。谁能帮助我并向我解释如何解决这个问题?谢谢! 感谢 Sanetro,我原来的问题得到了解决。现在发生了另一个错误。详细问题发布在 Sanetro 回答的评论部分。我来这里是为了包含我的详细编辑代码。
choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
Is_Current_One = True
won = False
wrongEntryPlayer1 = False
wrongEntryPlayer2 = False
while not won:
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
print('\n')
print('|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
print('----------')
print('|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
print('----------')
print('|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
# above code is to print board layouts
if Is_Current_One:
print("Player 1's turn.\nChoose a number to put an X there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Player 2's turn.\nChoose a number to put an O there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
if Is_Current_One:
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
else:
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
# code to toggle between True and False
if wrongEntryPlayer1 == False and wrongEntryPlayer2 == False:
Is_Current_One = not Is_Current_One
for pos_x in range(0, 3):
pos_y = pos_x * 3
# for row condition:
if (choices[pos_y] == choices[(pos_y + 1)]) and (
choices[pos_y] == choices[(pos_y + 2)]):
won = True
# column condition:
if (choices[pos_x] == choices[(pos_x + 3)]) and (
choices[pos_x] == choices[(pos_x + 6)]):
won = True
if ((choices[0] == choices[4] and choices[0] == choices[8])
or (choices[2] == choices[4] and choices[4] == choices[6])):
won = True
print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
[编辑 2] 好的,Sanetro,这实际上不是最终编辑。 我正在制作退出命令。当玩家输入 > 退出时,程序退出。我原本认为这会非常简单。你做一个while循环,当玩家输入退出时中断循环,然后就是这样!嗯,它不简单(至少对我而言)。由于choice = input(.....) in 循环,我不能将它放在循环之外(或在声明choice 变量之前),这很糟糕。我在顶部创建了一个新变量:exitTrigger 并将新行放入 while 循环中。它现在不起作用。当玩家输入退出时,它只是说请只输入有效的字段。可能是因为except语句仍然有效,我想添加一个条件来触发except语句,而我只想要exitTrigger为True时的except语句。我可以这样做吗?谢谢。
choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16']
Is_Current_One = True
won = False
wrongEntryPlayer1 = False
wrongEntryPlayer2 = False
exitTrigger = False
while not won and not exitTrigger:
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
print("Type exit to quit game")
print('\n')
print('|' + choices[0] + ' |' + choices[1] + ' |' + choices[2] + ' |' + choices[3] + ' |')
print('-------------')
print('|' + choices[4] + ' |' + choices[5] + ' |' + choices[6] + ' |' + choices[7] + ' |')
print('-------------')
print('|' + choices[8] + ' |' + choices[9] + '|' + choices[10] + '|' + choices[11] + '|')
print('-------------')
print('|' + choices[12] + '|' + choices[13] + '|' + choices[14] + '|' + choices[15] + '|')
# above code is to print board layouts
if Is_Current_One:
print("Player 1's turn.\nChoose a number to put an X there.")
try:
choice = int(input("> ").strip())
if choice == "exit":
exitTrigger = True
else:
wrongEntryPlayer1 = False
except ValueError:
if not exitTrigger:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
except IndexError:
if not exitTrigger:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
else:
print("Player 2's turn.\nChoose a number to put an O there.")
try:
choice = int(input("> ").strip())
if choice == "exit":
exitTrigger = True
else:
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
except IndexError:
if not exitTrigger:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
if not exitTrigger:
if Is_Current_One:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
except: # <============ HERE
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer1 = True
continue
else:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
except: # <============ HERE
print("Please enter only valid fields from board (1-16)")
wrongEntryPlayer2 = True
continue
# code to toggle between True and False
if not exitTrigger:
if wrongEntryPlayer1 == False and wrongEntryPlayer2 == False:
Is_Current_One = not Is_Current_One
for pos_x in range(0, 4):
pos_y = pos_x * 4
# for row condition:
if (choices[pos_y] == choices[(pos_y + 1)]) and (
choices[pos_y] == choices[(pos_y + 2)]) and (
choices[pos_y] == choices[(pos_y + 3)]):
won = True
# column condition:
if (choices[pos_x] == choices[(pos_x + 4)]) and (
choices[pos_x] == choices[(pos_x + 8)]) and (
choices[pos_x] == choices[(pos_x + 12)]):
won = True
if ((choices[0] == choices[5] and choices[0] == choices[10] and choices[0] == choices[15])
or (choices[3] == choices[6] and choices[3] == choices[9] and choices[3] == choices[12])):
won = True
if won:
print('\n')
print('|' + choices[0] + ' |' + choices[1] + ' |' + choices[2] + ' |' + choices[3] + ' |')
print('-------------')
print('|' + choices[4] + ' |' + choices[5] + ' |' + choices[6] + ' |' + choices[7] + ' |')
print('-------------')
print('|' + choices[8] + ' |' + choices[9] + '|' + choices[10] + '|' + choices[11] + '|')
print('-------------')
print('|' + choices[12] + '|' + choices[13] + '|' + choices[14] + '|' + choices[15] + '|')
# above code is to print board layouts
print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
if exitTrigger:
print("Game quited")
顺便说一下,游戏板现在是 4*4 :D
答案 0 :(得分:2)
您的代码实际上有两个问题——首先,您寻求帮助的问题是由切换标志 Is_Current_One
引起的,即使玩家输入“错误”输入也是如此。换句话说,当玩家一输入“asdf”时,我们希望他们有机会再次输入“好”的输入,而不是立即切换到玩家二。在读取 if
的行上放置一个 if not wrongEntryPlayer1 and not wrongEntryPlayer2:
块。
第二个更大的问题是您对玩家输入是否为 int
的类型检查不起作用。首先,choices[choice - 1] == int
将数组中的值与类型进行比较,该类型始终为 False
。其次,即使函数 isinstance(choices[choice-1], int)
也不起作用,因为 options 的每个成员都是字符串类型。因此,您应该使用字符串方法 choices[choice-1].isnumeric()
,它将确定数组中的字符串是否为数字。 (或者,您可以测试它不是 x 或 o,但这更像是 Python 风格。)
我测试了这些更改,它们似乎有效。 pastebin
答案 1 :(得分:1)
[编辑] 我做了一些修复,它工作正常。
choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
Is_Current_One = True
won = False
wrongEntryPlayer1 = False
wrongEntryPlayer2 = False
while not won:
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
print('\n')
print('|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
print('----------')
print('|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
print('----------')
print('|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
# above code is to print board layouts
if Is_Current_One:
print("Player 1's turn.\nChoose a number to put an X there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Player 2's turn.\nChoose a number to put an O there.")
try:
choice = int(input("> ").strip())
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
if Is_Current_One:
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
else:
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
# code to toggle between True and False
if wrongEntryPlayer1 == False and wrongEntryPlayer2 == False:
Is_Current_One = not Is_Current_One
for pos_x in range(0, 3):
pos_y = pos_x * 3
# for row condition:
if (choices[pos_y] == choices[(pos_y + 1)]) and (
choices[pos_y] == choices[(pos_y + 2)]):
won = True
# column condition:
if (choices[pos_x] == choices[(pos_x + 3)]) and (
choices[pos_x] == choices[(pos_x + 6)]):
won = True
if ((choices[0] == choices[4] and choices[0] == choices[8])
or (choices[2] == choices[4] and choices[4] == choices[6])):
won = True
print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
所以,我想为一些错误道歉。
我在互联网上找到了解决方案。在行 35 if choices[choice - 1] == int:
您必须像这样更改 if choices[choice - 1].isnumeric():
,大多数变量都被视为对象。如果您想尝试使用 type()
,您将收到错误消息。
接下来我删除了我的 choices_closed
这些东西 '='。这是完全没有必要的。你的想法比我做的更好。我说的是条件 if it's different then intiger it's fill'
。我没有看到这些代码行:
# code to toggle between True and False
Is_Current_One = not Is_Current_One
我立即写道:
# code to toggle between True and False
if wrongEntryPlayer1 == False and wrongEntryPlayer2 == False:
Is_Current_One = not Is_Current_One
我以为你和我一样困了,你没有注意到这一点。 我必须说这是我的错误
[编辑] 我在这里添加了一些例外:
if Is_Current_One:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'X'
wrongEntryPlayer1 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer1 = True
except: # <============ HERE
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer1 = True
continue
else:
try: # <============ HERE
if choices[choice - 1].isnumeric():
try:
choices[choice - 1] = 'O'
wrongEntryPlayer2 = False
except ValueError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
except IndexError:
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
else:
print("Please choose a number that is empty.")
wrongEntryPlayer2 = True
except: # <============ HERE
print("Please enter only valid fields from board (0-8)")
wrongEntryPlayer2 = True
continue
也许这是最终编辑:P 当你发现更多东西时,我会尽力提供帮助。
答案 2 :(得分:0)
我认为而不是使用
if not wrongEntryPlayer1 and not wrongEntryPlayer2:
你应该使用:
if wrongEntryPlayer1 or wrongEntryPlayer2:
这将修复印刷电路板的部分。