2017-05-07 47 views
0

我希望它说欢迎,要求用户输入(a,b,c),验证用户输入,如果验证返回输入是合理的,那么在a,b,c上执行二次公式。我怀疑问题出在while循环中。该程序只是欢迎,请求输入然后再说欢迎等。为什么下面的代码不遵循指定的顺序?

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) 
+5

里面的'number_check'功能,'check'是一个局部变量。分配给它不会改变全局变量。 – Barmar

+0

此外,'=='是一个比较,而不是任务 –

回答

1

你对你的怀疑是正确的。您在while循环中遇到问题。不符合你的代码假定的方式。

相反,你需要写类似:

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) 

注意,在除了改变while循环中,您还需要更新number_check在呼叫范围不会更新输入参数。相反,函数必须显式返回更新的值。

+0

已经在评论中回答.... –

+2

@GautamKrishnaR ...当我开始写我的答案时,这是不可用。而且,答案不应该在评论中。 – JohanL

1

number_check函数中使用check变量的方式有两个问题。

首先,由于您使用==(它测试相等)而不是=,因此您不会为其分配新值。

但是,因为它是一个参数变量,所以它是本地函数。因此,在函数内分配它不会修改您在while循环中测试的全局变量。您可以直接测试number_check的结果,而不是使用全局变量,并且在想要结束循环时使用break

如果你做出这种改变,你需要将呼叫转移到quadratic_formulaelse:条款,因为当while条件失败,而不是当我们结束与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) 
+0

如果这个时间是有意的呢? –

+0

我编辑了我为什么删除'else:'的解释。它不会与'break'一起工作。 – Barmar

1

尝试使用return,而不是尝试修改全局变量。

有一种方法可以使用全局变量(请参阅global语句),但这不是必需的代码。

检查参数本身是不是真的有必要,但

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 
相关问题