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. I have made a snake.py file which is the client and a server.py file for this purpose. 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.

Here is a link to my code where you should only open server_multi.py and server.py.

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/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. I have already implemented threading and my server is somehow sending bizzare data while my client is unable to parse it. Please point out where i am making a mistake in my implementation. _

1 answer

  • answered 2019-05-15 03:41 Ashutosh Mishra

    >>>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.

    >>> 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