简体   繁体   English

如何在我的代码中使用基数10修复“ValueError:int()的无效文字:''”

[英]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.

相关问题 如何修复&#39;ValueError:以10为基数的int()无效文字:&#39; - How to fix 'ValueError: invalid literal for int() with base 10:' 如何解决这个问题:ValueError:int() 的无效文字,基数为 10: - How to fix this: ValueError: invalid literal for int() with base 10: 如何修复 ValueError:int() 的无效文字,基数为 10:''? - How do i fix ValueError: invalid literal for int() with base 10: ''? ValueError: invalid literal for int() with base 10: '', 请帮助修复它 - ValueError: invalid literal for int() with base 10: '', pls help to fix it 如何使用基数 10 修复 int() 的无效文字:&#39;&#39;? - How to fix invalid literal for int() with base 10: ''? 我的代码是错误的(将str(1元素)的列表转换为int)错误ValueError:int()的常量文字以10为底:“ [&#39;2&#39;]” - my code is wrong (list of str(1 element) into a int) error ValueError: invalid literal for int() with base 10: “['2']” ValueError:int()的无效文字,基数为10:``在python代码中 - ValueError: invalid literal for int() with base 10: '' in python code ValueError:以 10 为基数的 int() 的无效文字:在 Visual Studio 代码中 - ValueError: invalid literal for int() with base 10: in Visual studio code ValueError:int() 的无效文字,我的字符串以 10 为底 - ValueError: invalid literal for int() with base 10 for my string 如何修复“ValueError: invalid literal for int() with base 10: &#39;&#39;发生。无论何时需要pyobjc,包括安装pyobjc - How to fix "ValueError: invalid literal for int() with base 10: '' occuring. whenever pyobjc is needed including installing pyobjc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM