我正在实施基于服务器-客户端的 TictacToe 。但我收到错误
I am implementing a server-client based TictacToe . But I am getting errors
In server.py [server.py][1] / 当我点击游戏方块进行移动时出现以下错误.
File "server.py", line 36, in run
self.connection.send((self.mark) )
TypeError: a bytes-like object is required, not 'str'
Server.py: [1]: http://pastebin.com/HR3DC6Kd
在 client.py [client.py][1] 中出现以下错误:
File "client.py", line 132, in sendClickedSquare
if self.myTurn:
AttributeError: 'TicTacToeClient' object has no attribute 'myTurn'
client.py [1]: http://pastebin.com/dexntYKx
我正在使用 python3。如何解决这个问题?我什么都试过了。
此代码用于 Python2,其中 string
和 bytes
没有区别,但 Python3 需要从 [=11= encode()
] 到 bytes
和 decode()
从 bytes
到 string
因为您不使用本地字符,所以您可以 decode/encode 使用 ascii
而不是 utf-8
或其他
value = self.connection.recv(1).decode('ascii')
self.connection.send(value.encode('ascii'))
顺便说一句:之后你使用 myTurn
就没有问题了,因为上面的问题产生了异常并且程序无法设置 self.myTurn = 0
In server.py [server.py][1] / 当我点击游戏方块进行移动时出现以下错误.
File "server.py", line 36, in run
self.connection.send((self.mark) )
TypeError: a bytes-like object is required, not 'str'
Server.py: [1]: http://pastebin.com/HR3DC6Kd
在 client.py [client.py][1] 中出现以下错误:
File "client.py", line 132, in sendClickedSquare
if self.myTurn:
AttributeError: 'TicTacToeClient' object has no attribute 'myTurn'
client.py [1]: http://pastebin.com/dexntYKx
我正在使用 python3。如何解决这个问题?我什么都试过了。
此代码用于 Python2,其中 string
和 bytes
没有区别,但 Python3 需要从 [=11= encode()
] 到 bytes
和 decode()
从 bytes
到 string
因为您不使用本地字符,所以您可以 decode/encode 使用 ascii
而不是 utf-8
或其他
value = self.connection.recv(1).decode('ascii')
self.connection.send(value.encode('ascii'))
顺便说一句:之后你使用 myTurn
就没有问题了,因为上面的问题产生了异常并且程序无法设置 self.myTurn = 0