Using boolean operators in an if statement

时间:2016-07-11 18:59:05

标签: python if-statement calculator

I'm a beginner in coding, and I'm trying to create a quadratic equation calculator using 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.")

This code keeps returning "Please enter numerical values." Why is the code not making it past the first "if" statement?

2 个答案:

答案 0 :(得分:8)

You're not calling those methods:

c.isalpha()
#        ^^

Note that a method or function in Python has a truthy value:

>>> bool(''.isalpha)
True

So not a.isalpha (and others) will always evaluate to False and the condition will never pass

答案 1 :(得分:0)

你必须在a.isalpha周围加上括号 - 就像这样a.isalpha()这会调用方法,而不是在它之前。您必须为b和c执行此操作

更新

对不起,我刚才意识到有人回答了这个问题,就像我在我面前一样,请不要接受我的回答并接受他们的回答!