【发布时间】:2021-07-19 18:06:31
【问题描述】:
我有一个类,我试图在其中创建一个新方法 move_piece() 。这是我当前的代码:
def move_piece(self):
propinfo = open('D:\pythonProject\monop\propertyinfo.txt')
propinfolines = propinfo.readlines()
temp = (propinfolines[self.squareID + 1].split(',')) # ^ all code above this is simply reading a specific line from a text file , and splitting it into an array by a delimited character (,)
print('x = ' + temp[12])
print('y = ' + temp[13]) # these two lines here are simply to print the new x and y value to the console to make sure I have the correct values.
x = temp[12]
y = temp[13]
self.rect.y = y
self.rect.x = x
当我运行代码时,我收到以下错误:
File "D:\monop\car.py", line 38, in move_piece
self.rect.x = x
TypeError: invalid rect assignment
我不确定为什么,从我读到的内容中,您不能直接从数组为 rect 赋值,因此我首先将它传递给变量。但它仍然无法正常工作。感谢您的帮助。
【问题讨论】: