简体   繁体   English

为什么以下代码不遵循指定的顺序?

[英]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. 我想说说欢迎,要求用户输入(a,b,c),验证用户输入,如果验证返回输入是合理的,则对a,b,c执行二次公式。 I suspect the problem is in the while-loop. 我怀疑问题出在while循环中。 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)

You are correct in your suspicion. 您的怀疑是正确的。 You have a problem in your while loop. 您的while循环中有问题。 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. 请注意,除了更改while循环外,您还需要更新number_check因为在调用范围中不会更新输入参数。 Instead the function has to explicitly return the updated value. 相反,该函数必须显式返回更新后的值。

There are two problems with the way you're using the check variable in the number_check function. number_check函数中使用check变量的方式存在两个问题。

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. 因此,在函数内部分配它不会修改您在while循环中测试的全局变量。 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. 除了使用全局变量,您还可以直接直接测试number_check的结果,并在想要结束循环时使用break

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 . 如果进行此更改,则需要将对quadratic_formula的调用移出else:子句,因为只有在while条件失败时才执行该调用, 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)

Try using return , not attempting to modify a global variable. 尝试使用return ,而不是尝试修改全局变量。

There's a way to use global variables (see global statement), but it's not necessary for this code. 有一种使用全局变量的方法(请参阅global语句),但是此代码不是必需的。

The check variable itself isn't really necessary, though 不过,check变量本身并不是必需的

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM