【问题标题】:Types complex vs int复杂类型与 int 类型
【发布时间】:2016-11-20 08:29:57
【问题描述】:
a = int(input("Please enter the value of a: "))
b = int(input("Please enter the value of b: "))
c = int(input("Please enter the value of c: "))

root_1 = (-b + ((b**2) - 4*a*c)**0.5) / 2*a
root_2 = (-b - ((b**2) - 4*a*c)**0.5) / 2*a

if root_1 < 0 or root_2 < 0:
   root = "No real roots"
elif root_1  > 0 or root_2 > 0:
   root = "Two real roots"
elif root_1 == 0 or root_2 == 0:
   root = "One real root"
print("The values you entered have", root)

您好,我遇到了 complex 和 int 的问题,这给了我一个错误。这个问题有方法解决吗?提前致谢。

【问题讨论】:

  • 你有什么问题?你在哪里使用complex?错误是什么?
  • TypeError: unorderable types: complex()
  • 当我输入 a=2、b =2 和 c=2 时,结果是一个复数,并给出了我在上述消息中粘贴的错误。
  • 你应该检查b^2 - 4*a*c是否大于0,而不是根。
  • 好的,谢谢。我会试一试的。(我应该知道的)

标签: python types int


【解决方案1】:

整个问题在于您为决定等式的结果而实施的逻辑......

您假设这是在quadratic equation 中区分根源的术语: root_1root_2

但这就是告诉你关于真正根源与否的原因:

(b**2) - 4*a*c)

这叫做判别式

并计算为

如果(b**2) - 4*a*c) 为正数,则平方根为实数,但如果表达式为负数,则平方根将产生一个虚数...

【讨论】:

    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2012-06-30
    相关资源
    最近更新 更多