0

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?

4
  • Brackets. You need each boolean if section to have brackets () around it. Commented Jul 11, 2016 at 18:59
  • try if (not a.isalpha()) and (not b.isalpha()) and (not c.isalpha()): Commented Jul 11, 2016 at 19:00
  • 7
    You need to call a.isalpha(). Commented Jul 11, 2016 at 19:01
  • 1
    ps: you do not need brackets around the if tests (not binds stronger than and) Commented Jul 11, 2016 at 19:03

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I'm trying to do, since I need numerical input.
0

You must put brackets around the a.isalpha - like this a.isalpha () this calls the method whereas before it didn't. You must do it for b and c as well

UPDATE

Sorry I just realised someone has answered this like I have way before me, please don't accept my answer and accept theirs!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.