2017-05-07 74 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 
相關問題