这是我的代码。
import math
print('I will solve the equation ax^2=bx=c=0')
a = int(input('a= '))
b = int(input('b= '))
c = int(input('c= '))
d=b**2-4*a*c
if d < 0:
print('The equation has no solution')
elif d == 0:
x=(-b)/(2*a)
print('This equation has one solution',x)
else:
x1 = (-b+math.sqrt(d))/(2*a)
x2 = (-b-math.sqrt(d))/(2*a)
print('This equation has two solutions: ',x1, 'or',x2)
我试过 guess.Number() 但我的逻辑不正确,这是第 4 章的 Python 作业,它要求用户尝试二次方程的多个输入。我对此很陌生。
欢迎来到 Stack Overflow。您正在寻找的内容可以通过 while 循环解决:https://www.w3schools.com/python/python_while_loops.asp
这是您的代码的解决方案:
import math
print('I will solve the equation ax^2 + bx + c = 0')
# Start a loop that will continue until the user chooses to exit
while True:
a = int(input('a= '))
b = int(input('b= '))
c = int(input('c= '))
d = b**2 - 4*a*c
if d < 0:
print('The equation has no solution')
elif d == 0:
x = (-b) / (2*a)
print('This equation has one solution', x)
else:
x1 = (-b + math.sqrt(d)) / (2*a)
x2 = (-b - math.sqrt(d)) / (2*a)
print('This equation has two solutions: ', x1, 'or', x2)
# Ask the user if they want to solve another equation
user_input = input('Do you want to solve another equation? (yes/no): ')
# If the user types 'no' (in any case), break out of the loop and end the program
if user_input.lower() == 'no':
break