python - 如何修复TypeError : unsupported operand type(s) for -: 'tuple' and 'tuple'

标签 python compiler-errors

即时通讯试图使一个蛇AI,可以看到自己在哪里,所以即时通讯使用此代码来尝试做到这一点

我是python的新手

                if new_direction == "Right" and new_head_position - (0, 20) in self.snake_positions:
                    if new_direction == "Right" and new_head_position + (0, 20) in self.snake_positions:
                        self.direction = "Right" 

我希望代码仅在其他空间被蛇形部分占据时将self.direction设置为“Right”,但是我收到此错误

最佳答案

以下表达式将产生错误:new_head_position - (0, 20)new_head_position + (0, 20),因为Python不允许您在元组之间执行算术运算。

这是适合您的解决方法:

(new_head_position[0], new_head_position[1] - 20)
(new_head_position[0], new_head_position[1] + 20)

关于python - 如何修复TypeError : unsupported operand type(s) for -: 'tuple' and 'tuple' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58211289/

相关文章:

.net - Visual Studio 2008 中的错误编译程序

python - 在 Google App Engine 上使用 Mandrill/MailChimp 发送确认邮件

python - Spyder IDE 控制台历史

python - Redshift 中的长真空

build - fatal error : vector: No such file or directory

winapi - LockWorkStation-编译错误-找不到标识符

Haskell 类型错误 : Could not deduce (Show a) arising from a use of `show' from the context (Num a)

python - 多标签分类

python - 将 pandas 中的月份数字转换为日期时间

xcode - 使用Xcode测试为Objective-C++代码库设置单元