[英]How to fix the “ ValueError: invalid literal for int() with base 10: ' ' ” in my code
I am actually implementing a classic multiplayer game using python curses library. 我实际上正在使用python curses库实现一个经典的多人游戏。 I have made a
snake.py
file which is the client and a server.py
file for this purpose. 为此,我创建了一个
snake.py
文件,它是客户端和server.py
文件。 I have tried looking at the data and making sure I am parsing the received data correctly and sending it correctly to make sure I am not sending an empty string but still on the snake.py
the position that my server sends in the start is an empty string which the function parse cannot convert since int()
cannot convert an empty string. 我已经尝试查看数据并确保正确解析收到的数据并正确发送以确保我没有发送空字符串但仍然在
snake.py
上我的服务器在开始时发送的位置是空的函数解析无法转换的字符串,因为int()
无法转换空字符串。
Here is a link to my code where you should only open server_multi.py and server.py. 这是我的代码的链接,您只应该打开server_multi.py和server.py。
https://github.com/hamza24farrukh/Netcen-cs-382/tree/master/assignment2/snake%20IO https://github.com/hamza24farrukh/Netcen-cs-382/tree/master/assignment2/snake%20IO
and here are the two links to the pictures of both of my terminals https://imgur.com/b2Anuw5 以下是我的两个终端https://imgur.com/b2Anuw5图片的两个链接
https://imgur.com/dtybwtW https://imgur.com/dtybwtW
The main question is how do i fix my code so that it starts sending and receiving data between two clients which is when i open two instances of my snake_multi.py file. 主要问题是我如何修复我的代码,以便它开始在两个客户端之间发送和接收数据,这是当我打开我的snake_multi.py文件的两个实例时。 I have already implemented threading and my server is somehow sending bizarre data while my client is unable to parse it.
我已经实现了线程,我的服务器以某种方式发送奇怪的数据,而我的客户端无法解析它。 Please point out where i am making a mistake in my implementation.
请指出我在实施中出错的地方。
>>>int('33')
33
>>>int('33.44')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '33.44'
So, python recognises an integer string, but it raises a ValueError
if it get an invalid string or a float value. 因此,python识别整数字符串,但如果它获得无效字符串或浮点值,则会引发
ValueError
。
>>> int('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
So, this is the error in your code. 所以,这是代码中的错误。 Check for this.
检查一下。
You can also use 你也可以使用
try:
int('') #or your code
except ValueError:
pass # or whatever
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.