Python Forum
int() argument Error.... but it's not :)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
int() argument Error.... but it's not :)
#1
Hi there,
my code is totally fine but only during day time :)
I basically do a GET request to a solar inverter that responds (during day time!) with the given energy the sun produces.
The code runs in a loop and checks this data every 30 seconds. This data is a number value and is saved into a variable.
Because I need to calculate something with these numbers I have to change the variable into an int(xxx).
The problem now is, that as soon as the sun is gone, the inverter does not respond with an "0" (for zero energy) but with a text "null".
That means that every day when the sun is gone, my code crashes giving me an int error:
TypeError: int() argument must be a string or a number, not 'NoneType'
Maybe someone of you has an idea to solve this problem.
I would prefer if the "null" would just be changed into an "0". As such my whole integration would work flawless again.
Thanks a lot for your help!
Reply
#2
Why don't you just check the value before trying to pass it to int?
Reply
#3
or use try/except TypeError to handle the exception

and by the way, are you sure you need to convert to int. In the JSON response you show in your other thread the numeric values are converted to int/float. They are not strings. Still null will be converted to None so you still need to do some checks, but no need to convert
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange argument count error rowan_bradley 3 660 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 5,723 May-09-2022, 12:49 PM
Last Post: deanhystad
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,737 Mar-03-2020, 08:34 AM
Last Post: buran
  Syntax Error with Argument Parser aditya_man 1 3,870 Sep-30-2019, 01:21 PM
Last Post: ichabod801
  Invalid argument error thrown. pyseeker 4 8,570 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 21,691 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  missing 1 required positional argument error nikos 3 7,394 Feb-28-2019, 12:53 PM
Last Post: nikos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020