我在这个项目中工作,我检查了一个完成的井字游戏。用户在每行(共3行)中输入x、o和句点(最多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")如果我插入了一个无效的符号,那么它将打印该语句。
发布于 2013-10-15 02:54:37
您似乎误解了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')的时候会发生什么?这个列表是什么样子的?
https://stackoverflow.com/questions/19366969
复制相似问题