为什么我总是收到ELIF无效语法错误?

2024-09-30 04:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

遇到一个ELIF语法错误,我是python新手,不知道为什么会遇到这个错误

print("Résolution de l'équation du second degré : ax² + bx + c = 0")
chaineA= input("Coefficient de a ?: ")
a= float(chaineA)
chaineB= input("Coefficient de b ?: ")
b= float(chaineB)
chaineC= input("Coefficient de c ?: ")
c= float(chaineC)
delta=((b**2)-(4*a*b))
solut1= (-b + (delta**0.5)/(2*a))
solut2= (-b - (delta**0.5)/(2*a))
solut3= (-b/2*a)

if delta >= 0.0:
    print("Deux solutions: ")
    print("x1 =" , solut1)
    print("x2 =" , solut2)
elif delta = 0.0:
    print("Une solution")
    print("x =" , solut3)
elif delta <= 0.0:
    print("Pas de solution")

提前谢谢


Tags: inputdefloatdeltaprintsolutionelifcoefficient
3条回答
print("Résolution de l'équation du second degré : ax² + bx + c = 0")
chaineA= input("Coefficient de a ?: ")
a= float(chaineA)
chaineB= input("Coefficient de b ?: ")
b= float(chaineB)
chaineC= input("Coefficient de c ?: ")
c= float(chaineC)
delta=((b**2)-(4*a*b))
solut1= (-b + (delta**0.5)/(2*a))
solut2= (-b - (delta**0.5)/(2*a))
solut3= (-b/2*a)

if delta >= 0.0: # here it should be > only
    print("Deux solutions: ")
    print("x1 =" , solut1)
    print("x2 =" , solut2)
elif delta = 0.0: # first error here it should be elif delta == 0:
    print("Une solution")
    print("x =" , solut3)
else: #this could be made else. no need to put a condition.
    print("Pas de solution")

您使用了单=而不是==

elif delta == 0.0:

通过在elif中仅使用一个=,它认为您正在尝试将0.0分配给增量。应该是:

elif delta == 0.0

这将比较delta和0.0,如果它们相同,则在elif中运行代码

相关问题 更多 >

    热门问题