return None 在程序结束时 python

return None in the end of the program in python

在这个问题中,他们让我从用户那里获取数字,然后求解一个二次方程。 我做到了,并在 Python 导师上检查过,每次 return 我 None,这是为什么?

def quadratic_equation(a,b,c):
import math
if a == 0:
    print("The parameter 'a' may not equal 0")
elif (b**2) - (4 * a * c) < 0:
    print("The equation has no solutions")
else:
### x1 mean with +
    x1 = ((-b) + (math.sqrt((b**2) - (4 * a * c)))) / (2 * a)
### x2 mean with - 
    x2 = ((-b) - (math.sqrt((b**2) - (4 * a * c)))) / (2 * a)
    if x1 and x2 is not None:
        return (f"The equation has 2 solutions: {x1} and {x2}")
    elif x1 is None:
        return (f"The equation has 1 solution: {x2} ")
    elif x2 is None:
        return (f"The equation has 1 solution: {x1} ")

def quadratic_equation_user_input():
numbers = (input("Insert coefficients a, b, and c: "))
num = []
for i in numbers.split():
    try:
        num.append(float(i))
    except ValueError:
        pass
a = num[0]
b = num[1]
c = num[2]
quadratic_equation(a,b,c)


print(quadratic_equation_user_input())

quadratic_equation_user_input 调用 quadratic_equation 并忽略它的 return 值,因此它 returns None 这是默认的 return 值。

# Call function but ignore return value
quadratic_equation(a,b,c)

# No return in caller function -> default to None
# ....

你的意思可能是

return quadratic_equation(a,b,c)

quadratic_equation_user_input 的末尾。