我正在为一个班级项目写一个tic tac脚趾游戏。基本上我有这个清单:
grid = [[1,2,3],[4,5,6],[7,8,9]]
我只想根据玩家的不同,用X或O更新列表值。我试图使用以下函数来实现,其中p是从用户输入的整数(是的,我确保输入实际上是整数而不是字符串):
^{pr2}$当我运行此代码时,我会发现一个错误,即列表索引必须是整数而不是列表类型。或者,我尝试用这行替换变量c,而不是^{{cd1>}
^{pr3}$打印c的值和类型,显示c为int,并与用户输入的值匹配。所以我可以找到变量,类型匹配,我只是无法更新原始列表中的值。我是否错误地使用全局变量?我搞不懂为什么它不会更新。以下是迄今为止的整个计划:
^{pr4}$
循环中的}是行和列,而不是它们的索引。您需要跟踪索引以更新值。您可以使用enumerate function执行此操作。在
r
和{相关问题 更多 >
编程相关推荐