Khaozz
Khaozz

Reputation: 3

Python | TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' |use Variable to divide

a = int(float(input("Add : ")))
b =  int(float(input("Add : ")))
c = print(" = " , a**2 + b**2)

print(c/2)

I'm going to divide the variable and the number but it happens like this, how do I fix it? | TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

Upvotes: 0

Views: 462

Answers (2)

Brian Rodriguez
Brian Rodriguez

Reputation: 4359

print returns None, which you assign to c. None can't be divided by an int, hence your error.

If you want to store the result of a**2 + b**2 in c, then do so directly:

a = int(input("Add: "))
b = int(input("Add: "))
c = a**2 + b**2

print(f"{a=}, {b=}, {c=}")
print(c / 2)

Sample execution:

Add: 3
Add: 5
a=3, b=5, c=34
17.0

Upvotes: 2

Hatatister
Hatatister

Reputation: 1052

print is a function which returns None and the side effect of printing its args to stdout. Therefore c has the value None. And None / 2 gives the TypeError

Upvotes: 0

Related Questions