Python Forum

Full Version: python nonetype variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello I have the following in a threaded function executes several at the same time
profit is global variable

profit += result
profit = round (profit, 2)

and there is no way to get it when you run 2 simultaneous error

python TypeError: unsupported operand type (s) for +: 'int' and 'NoneType'

what can I do

if profit is None:

profit = 0

did not solve my problem, the profit variable is profit = 0 before the function and does not work if it is not simultaneous, only 2 theads calling the function there of the error
you could use:
try:
    profit += result
except TypeError:
    profit = 0
But it's much better pre-initialize both variables to zero:

result = 0
profit = 0
# ... do some stuff
profit += result
[/python]
(Nov-12-2020, 01:35 AM)Larz60+ Wrote: [ -> ]you could use:
try:
    profit += result
except TypeError:
    profit = 0
But it's much better pre-initialize both variables to zero:

result = 0
profit = 0
# ... do some stuff
profit += result
[/python]


[python]
result = 0
profit = 0


this treads dont set 0, is SUM of the 2 funciton simultaneos
Not sure what you're asking.
Quote:python TypeError: unsupported operand type (s) for +: 'int' and 'NoneType'

Note the order. "int" is first and "NoneType" is second. That reflects the order of the expression as well.

Quote:profit += result

If it's for this line, then it is result that is none in the expression, not profit.
you should always initialize variables to some finite state.

or if not, at least capture any exceptions, otherwise at some point your program may crash and it may only do so when something very unusual happens, perhaps several months after software has been released.