python - Unsupported operand type(s) for +: 'NoneType' and 'str' and 'int' -


code:

string1 = " right" g = lambda x: x ** 2  print(g(8)) + str(string1) 

error:

traceback (most recent call last):   file "c:/users/computer/desktop/testing.py", line 3, in <module>     print(g(8)) + str(string1) typeerror: unsupported operand type(s) +: 'nonetype' , 'str' 

code2: tried adding too:

g = lambda x: x ** 2  + " should right!" print(g(8)) 

error:

traceback (most recent call last):   file "c:/users/computer/desktop/testing.py", line 2, in <module>     print(g(8))   file "c:/users/computer/desktop/testing.py", line 1, in <lambda>     g = lambda x: x ** 2  + " should right!" typeerror: unsupported operand type(s) +: 'int' , 'str' 

i tried int , str still had problems?

also when fixing, please explain how fixed code works :) dont want copy fixed line

you adding result of print() , str(), print() returns none.

you wanted print result of adding g(8) , str() instead, you'll have turn return value of g(8) string too:

print(str(g(8)) + str(string1)) 

note placement of closing ) print() function!

the second str() call not needed @ all, because string1 string:

print(str(g(8)) + string1) 

you can leave print() passing in values separate arguments instead:

string1 = "is right" print(g(8), string1) 

i removed leading space string1 because print() will, default, insert spaces between arguments being printed.

demo:

>>> string1 = "is right" >>> g = lambda x: x ** 2  >>> print(g(8), string1) 64 right 

your second attempt tried move string concatenation g lambda; in case you'd have turn result of x ** 2 string first:

g = lambda x: str(x ** 2)  + " should right!" 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -