【问题标题】:Why does the following code not follow the specified order?为什么下面的代码不遵循指定的顺序?
【发布时间】:2017-05-07 16:17:08
【问题描述】:

我希望它说欢迎,询问用户输入 (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)

【问题讨论】:

  • number_check函数内部,check是一个局部变量。分配给它不会改变全局变量。
  • 另外,== 是一个比较,而不是一个赋值

标签: python while-loop


【解决方案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,因为输入参数不会在调用范围内更新。相反,该函数必须显式返回更新后的值。

【讨论】:

  • @GautamKrishnaR ...当我开始写我的答案时它不可用。而且,答案不应该出现在 cmets 中。
【解决方案2】:

尝试使用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 

【讨论】:

    【解决方案3】:

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

    首先,您没有为其分配新值,因为您使用的是==(测试相等性)而不是=

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

    如果您进行此更改,则需要将对quadratic_formula 的调用移出else: 子句,因为这仅在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)
    

    【讨论】:

    • 如果 while-else 是故意的怎么办?
    • 我已经编辑了我为什么要删除 else: 的解释。它不能与break 一起使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多