python - 将识别列表中的空字符串并在那里打印标记/符号的函数(Python Tic-Tac-Toe)
问题描述
在这里编码新手,在我的第一个 Python 井字游戏板上工作。
最近,我问了这个问题,那些回答的人非常有帮助。除了我的代码不正确/没有以可以返回我想要的结果的方式编写之外,我意识到我已经超越了自己。我需要先编写一个函数,让我的井字游戏中的两个玩家(使用一台计算机)轮流,我成功地完成了。然后我意识到我需要确定井字游戏板上是否有空闲空间来放置标记(“X”或“O”),我将其表示为:
def space_check(board, position):
return board[position] == ' '
space_check(test_board, 8)
问题:
现在,我很难编写一个函数来识别棋盘上的特定位置是空闲的,然后将玩家的标记(“X”或“O”)放在空白处。
尝试的解决方案(注意:下面的这些板是测试板,我使用'$'作为测试标记):
board = ['#','a','b','c','d','e','f','g','h',' ']
marker = "$"
position=0
def place_marker(board, marker, position):
# while our position is an acceptable value (an int between 1 and 9)
while position not in range(0,10):
position = int(input("Choose a number from 1 through 9: " ))
# at the board's position, place marker 'X' or 'O'
board[position] = marker
print(board)
place_marker(board, marker, position)
虽然这确实以列表的形式返回输出,但当我显示板时,板不受影响:
place_marker(board, marker, position)
display_board(board)
输出:
| |
g|$|$
| |
______
| |
d|e|f
| |
______
| |
a|$|c
| |
我也试过这个,但代码不会在 VSC(我用于这个项目的主要 IDE)中运行,即使它在 Jupyter notebook 中工作:
test_board = ['#','a','b','c','d','e','f','g','h',' ']
def player_choice(board):
position = 0
while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
position = int(input("Choose your next position (1-9): " ))
return position
player_choice(test_board)
输出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-169-72130d7eb126> in <module>
----> 1 player_choice(test_board)
<ipython-input-168-37d67e2d2b88> in player_choice(board)
7
8 while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):
----> 9 position = int(input("Choose your next position (1-9): " ))
10
11 return position
ValueError: invalid literal for int() with base 10: ''
我知道我在这里的理解存在差距,但我不知道我错过了什么或如何继续。被困在这里几天了,所以我很感激帮助!
更新:解决方案
# NEXT STEP: write a function that can check for input in acceptable range AND check for free space
def player_choice(board):
# while player is taking a turn
while True:
try:
# ask player for input
position = int(input("Choose a Number (1 -9): " ))
assert 0 < position < 10 # ensure that this input is within range
assert board[position] == ' ' # ensure that there is a free space on which to place the input as marker
except ValueError: # override the ValueError exception
print("You didn't enter a number. Try again!") # tell player that input was not in range
except AssertionError as e:
print(e)
else:
return position
解决方案
乍一看,如果int(input()
你的位置变量是一个空字符串(),它看起来会导致错误''
,这就是你得到的错误。相反,试试这个:
position = input("Choose Position (1-9): ")
try:
position = int(position) # Trying to make position into an integer
except: # If there is an error,
if position == '':
# Do whatever if position is empty
else: # This means the position is not an integer, and not an empty string
# Do something to force the player to enter an integer or an empty string
我们没有足够的代码来自己尝试这个,所以我将尝试从这里提供帮助:)
更新
marker = '$'
while True:
try:
position = int(input('Choose a Number (1 -9) '))
break
except ValueError:
if not position: # 'not' compares to an emptry value, or a False boolean.
# It's much clearer imo than == ''
position = marker
pass
在回应您的第三条评论时,NoneType
例外是当您有一个None
值时需要另一个值(字符串或整数)。当您收到错误时,您可以随时用 Google 搜索错误的含义。编程时要开发的另一件事是记住一些基本错误是什么,例如NoneType
和parsing error
。在您的情况下,类型错误NoneType
意味着None
您的列表中有一个值。
推荐阅读
- url - FFmpeg 到 Shoutcast - 正确的 URL 语法
- java - 这个结构有效吗?
- ios - 我无法在全局队列上运行进程(后台模式)
- node.js - ADB SHELL INPUT TEXT 结果中缺少字符
- vb.net - 从 VB.net 连接到 Teradata
- flutter - Flutter,FileReader()编译到Android时崩溃
- jquery - 将控制按钮定位在 div 元素的侧面
- java - java - 如何在Java运行时更改具体装饰器中的变量
- entity-framework - EF Core 3.1 Linq 查询失败 - 无法将“System.Int32”类型的对象转换为“System.Boolean”类型
- c++ - 解释这个排序算法是如何工作的?