Choconator
Choconator

Reputation: 1

Why does the following code not follow the specified order?

I want it to say welcome, ask for the user input (a,b,c), validate the user input and if the validation returns that the input is reasonable then carry out the quadratic formula on a,b,c. I suspect the problem is in the while-loop. The program just welcomes, asks for input then says welcome again and so on.

from math import sqrt

def quadratic_formula(a,b,c):
    a=float(a)                                      #The quadratic formula
    b=float(b)
    c=float(c)
    x1_numerator = -1*b + sqrt((b**2)-4*(a*c))
    x2_numerator = -1*b - sqrt((b**2)-4*(a*c))
    denominator = 2*a
    x1_solution = x1_numerator/denominator
    x2_solution = x2_numerator/denominator
    print("x= "+str(x1_solution)+" , x= "+str(x2_solution))

def number_check(a,b,c,check):                     #carries out a check 
    a=float(a)
    b=float(b)
    c=float(c)
    if (b**2)-4*a*c < 0:
        print("The values you have entered result in a complex solution. Please check your input.")
        check == False
    else:
        check == True

check = False

while check == False:
    print("Welcome to the Quadratic Equation Calculator!")
    a = input("Please enter the x^2 coefficient: ")
    b = input("Please enter the x coefficient: ")
    c = input("Please enter the constant: ")
    number_check(a,b,c,check)
else:
    quadratic_formula(a,b,c)

Upvotes: 0

Views: 70

Answers (3)

Barmar
Barmar

Reputation: 781129

There are two problems with the way you're using the check variable in the number_check function.

First, you're not assigning new values to it, because you're using == (which tests equality) rather than =.

But also, since it's a parameter variable, it's local to the function. So assigning it inside the function does not modify the global variable that you test in the while loop. Rather than use a global variable, you can simply test the result of number_check directly, and use break when you want to end the loop.

If you make this change, you need to move the call to quadratic_formula out of the else: clause, because that's only executed when the while condition fails, not when we end the loop with break.

def number_check(a,b,c):                     #carries out a check 
    a=float(a)
    b=float(b)
    c=float(c)
    if (b**2)-4*a*c < 0:
        print("The values you have entered result in a complex solution. Please check your input.")
        return False
    else:
        return True

while True:
    print("Welcome to the Quadratic Equation Calculator!")
    a = input("Please enter the x^2 coefficient: ")
    b = input("Please enter the x coefficient: ")
    c = input("Please enter the constant: ")
    if number_check(a,b,c):
        break

quadratic_formula(a,b,c)

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191738

Try using return, not attempting to modify a global variable.

There's a way to use global variables (see global statement), but it's not necessary for this code.

The check variable itself isn't really necessary, though

def number_check(a,b,c): 
    a=float(a)
    b=float(b)
    c=float(c)
    return (b**2)-4*a*c >= 0  # return the check

while True:
    print("Welcome to the Quadratic Equation Calculator!")
    a = input("Please enter the x^2 coefficient: ")
    b = input("Please enter the x coefficient: ")
    c = input("Please enter the constant: ")
    if not number_check(a,b,c):
        print("The values you have entered result in a complex solution. Please check your input.")
    else:
        break  # getting out of the loop 

Upvotes: 1

JohanL
JohanL

Reputation: 6891

You are correct in your suspicion. You have a problem in your while loop. does not work the way your code assumes.

Instead you need to write something like:

def number_check(a,b,c):                     #carries out a check 
    a=float(a)
    b=float(b)
    c=float(c)
    if (b**2)-4*a*c < 0:
        print("The values you have entered result in a complex solution. Please check your input.")
        check = False
    else:
        check = True
    return check

check = False

print("Welcome to the Quadratic Equation Calculator!")
while check == False:
    a = input("Please enter the x^2 coefficient: ")
    b = input("Please enter the x coefficient: ")
    c = input("Please enter the constant: ")
    check = number_check(a,b,c)
quadratic_formula(a,b,c)

Note, that in addition to changing the while loop you also need to update number_check as input parameters are not updated in calling scope. Instead the function has to explicitly return the updated value.

Upvotes: 1

Related Questions