在if语句中使用布尔运算符

Vla*_*byr 0 python if-statement calculator

我是编码的初学者,我正在尝试使用python创建二次方程计算器.

while True:
    print("""
        Welcome to DJIGURDA Supreme Quadratic Calculator
        Please enter three values in the form of /'ax^2 + bx +c/'. """)
    a = input("a (first constant)")
    b = input("b (second constant)")
    c = input("c (third constant)")

    if not a.isalpha and not b.isalpha and not c.isalpha:
        d = (float(b)**2) - (4 * float(a) * float(c))
        print(d)
        if d >= 0:
            x_1 = (float(-b) + (d**0.5)) / (2*float(a))
            x_2 = (float(-b) - (d**0.5)) / (2*float(a))
            print("The first variable is equal to %s./n The second variable is equal to %s")[str(x_1), str(x_2)]
        else: 
            print("No real roots.")
    else:
        print("Please enter numerical values.")
Run Code Online (Sandbox Code Playgroud)

此代码不断返回"请输入数值".为什么代码没有通过第一个"if"语句?

Mos*_*oye 8

你不是在调用这些方法:

c.isalpha()
#        ^^
Run Code Online (Sandbox Code Playgroud)

请注意,Python中的方法或函数具有truthy值:

>>> bool(''.isalpha)
True
Run Code Online (Sandbox Code Playgroud)

因此not a.isalpha(和其他人)将始终评估,False并且条件永远不会通过