你们可以帮我找到错误的代码部分吗?我尝试了一些东西并在这个网站上搜索,但我找不到解决方案..如果你们知道的话,请告诉我们:D
print("Uw formule is ax**2+bx+c")
a= float(input("Geef getal a:" ))
b= float(input("Geef getal b:" ))
c= float(input("Geef getal c:" ))
D=b**2-4*a*c
while (D<=0) or (a==0):
print("Voer nieuwe getallen in, de uitkomst is niet te berekenen")
a= float(input("Geef getal a:" ))
b= float(input("Geef getal b:" ))
c= float(input("Geef getal c:" ))
D=b*b-4*a*c
D= format(D,'.1f')
if (D>0):
print("\nDe discriminant is:", D )
x1= (-b-(D)**0.5)/(2*a)
x1= format(x1,'.1f')
x2= (-b+(D)**0.5)/(2*a)
x2= format(x2,'.1f')
print("De uitkomst van x1=", x1)
print("De uitkomst van x2=", x2)
然后是错误:
Traceback (most recent call last):
File "*****", line 15, in <module>
while (D<=0) or (a==0):
TypeError: unorderable types: str() <= int()
答案 0 :(得分:0)
在for循环结束时,您将D
重新分配给调用format
的结果。这将永远使它成为一个字符串。
我不确定你为什么这样做,但你应该删除该行。
答案 1 :(得分:0)
谢谢!我解决了它:
print("Uw formule is ax**2+bx+c")
a= float(input("Geef getal a:" ))
b= float(input("Geef getal b:" ))
c= float(input("Geef getal c:" ))
D=b**2-4*a*c
while (D<=0) or (a==0):
print("Voer nieuwe getallen in, de uitkomst is niet te berekenen")
a= float(input("Geef getal a:" ))
b= float(input("Geef getal b:" ))
c= float(input("Geef getal c:" ))
D=b*b-4*a*c
D1= format(D,'.1f') #this code
if (D>0):
print("\nDe discriminant is:", D1 )
x1= (-b-(D)**0.5)/(2*a)
x1= format(x1,'.1f')
x2= (-b+(D)**0.5)/(2*a)
x2= format(x2,'.1f')
print("De uitkomst van x1=", x1)
print("De uitkomst van x2=", x2)