我希望它說歡迎,要求用戶輸入(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'是一個局部變量。分配給它不會改變全局變量。 – Barmar
此外,'=='是一個比較,而不是任務 –