Invalid literal for int() with base 10:

i am trying to make a water level monitoring project,in this project i am using linux server to send alert to my phone through sms.i have connected ultrasonic sensor with arduino and connected the bolt with arduino through serial communication(Tx and Rx). i have given the code below pls check it. i always get this error(invalid literal for int() with base10) when i execute it.somebody pls help me to solve this problem.

Dear @viswamanii007 viswamanii007, it is good to know you are really working some good real world problem, keep it up!!

The error you are getting is type error, though it is not clear about data type of your unknown variables. But i think error might be in “data[‘value’]” that you are getting from json. I think “data[‘value’]” is a string something like “10cm” which can’t be simply converted into integer with int() you have to first split that into "10 and ‘cm’ " then apply int() on 10…
So, the simple solution would be just check the data type of “data[‘value’]” what it is returning if it is something that i stated above just follow those steps…even in your console i can see it is clearly written “WATER LEVEL: 10cm” which is example of that error…
Please let me know you solved that problem or not , BEST OF LUCK

1 Like

sir, thank you so much for helping me.As you said to check the data type,i have corrected it to integer “10” and string "cm"and corrected the program. It worked for a while,i mean the first data is recivied properly, and i got the message alert.but next the same problem rised again and started to continue. I am confused and once again restarted the linux server,and excute the program once again from the begining, the same problem started to rise,but sometimes during excuting it works properly , one or two times ,again the problem starts.Thank you for helping me. If you have any more suggestions, to make it work pls tell me.

@viswamanii007 , can you please share the screenshots of your changed code and error that you are getting now…

Error after correcting the program!!

COM5%207_22_2020%204_34_02%20PM

Before correcting the program

COM5%207_22_2020%204_23_43%20PM
after correcting the program

@viswamanii007 your python code please…

@viswamanii007 try this code for conversion…

data = ‘10cm’ # value returned from ( data[‘value’] )
water_lvl = data[:-2]
print('The Water level is: ’ + water_lvl + ‘cm’)

water_lvl = int(water_lvl)

try:

if(water_lvl < high):
    ...
    ...
    ...

except:

try these codes and let me know it is working or not…

sir,as u said ,i have tried the code suggested by you,its working perfectly.But, we declared water level is 10 cm,i considers water level as 10 cm throughout the program.It does not get any value from ultrasonic sensor, and gives the same output everytime it excutes. but the code works properly without any error.what can do to get the value from the sensor…

@viswamanii007 , i just gave an example with ‘data=10cm’ you don’t have to put 10cm in your code.
Put value that returned from data[‘value’] into data variable.
data = data[‘value’]

Computers store numbers in a variety of different ways. Python has two main ones. Integers, which store whole numbers (ℤ), and floating point numbers, which store real numbers (ℝ). You need to use the right one based on what you require. This error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that’s not an integer to the int() function . In other words it’s either empty, or has a character in it other than a digit.

You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

val = "10.10"
if val.isdigit():
  print(int(val))

The other way to overcome this issue is to wrap your code inside a Python try…except block to handle this error.

Or if you are trying to convert a float string (eg. “10.10”) to an integer, simply calling float first then converting that to an int will work:

output = int(float(input))