检查python上的tic tac toe游戏

时间:2013-10-14 18:41:41

标签: if-statement for-loop python-3.x

我正在研究这个项目,在那里我检查了一个完成的tic tac toe游戏。用户在每行中输入x和o的组合以及句点(最多3个)(有3行)。我必须检查x或o是否需要太多转弯。这就是我所拥有的:

if ttt.count(x) > 5:
                    print("x took too many turns")

这仅适用于x。当我运行程序并测试以查看此语句是否出现在x中时,并且没有任何内容出现。我做错了什么?

以下是一个示例运行:

0 - check a finished tic-tac-toe
1 - check a finished sudoku
2 - check a game of connect four
3 - quit
choose an option: 0
option 0
For each row, start with x. Enter a combination of x'sand o's up to three characters. For a blank space,enter a period '.'.
top row:xox
middle row:xox
bottom row:xox  
['xox', 'xox', 'xox']
0 - check a finished tic-tac-toe
1 - check a finished sudoku
2 - check a game of connect four
3 - quit
choose an option: 

这是代码的更大部分:

for x in i:
            if x not in valid_symbols:
                    print("invalid board - invalid symbol " + x )
                    done = True
                    break
            if ttt.count(x) > 5:
                print("x took too many turns")

如果我插入无效符号,则打印该语句。

1 个答案:

答案 0 :(得分:1)

您似乎误解了ttt的内容。打开解释器并尝试以下操作:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> tic_tac_toe = []
>>> tic_tac_toe.append(input('Top row: '))
Top row: xxx
>>>
>>> tic_tac_toe
['xxx']
>>> tic_tac_toe.count('x')
0
>>> tic_tac_toe.count('xxx')
1

.append('xox')时会发生什么?列表是什么样的?