【问题标题】:i made a mistake while trying to solve the quadratic formula in python我在尝试解决python中的二次公式时犯了一个错误
【发布时间】:2020-07-27 12:27:40
【问题描述】:

我是 python 的初学者,有人可以帮我看看我做错了什么吗?

import math


def quadratgleichung(a,b,c):
    loesung1 = - (b/a)/2 + sqrt(    ((b/a)/2) * ((b/a)/2) - (c/a)   )
    loesung2 = - (b/a)/2 - sqrt(    ((b/a)/2) * ((b/a)/2) - (c/a)   )

    return loesung1, loesung2

a = input("Enter a.")
b = input("Enter b.")
c = input("Enter c.")

quadratgleichung(a,b,c)

print(f"Die Lösungen der eingegebenen quadratischen Gleichung sind {loesung1} und {loesung2} !\n")

这是控制台中出现的内容: 我应该在函数中使用不同的变量名还是问题所在?

TypeError                                 Traceback (most recent call last)
<ipython-input-11-3207ceaf35b6> in <module>
     12 c = input("Enter c.")
     13 
---> 14 quadratgleichung(a,b,c)
     15 
     16 print(f"Die Lösungen der eingegebenen quadratischen Gleichung sind {loesung1} und {loesung2} !\n")

<ipython-input-11-3207ceaf35b6> in quadratgleichung(a, b, c)
      3 
      4 def quadratgleichung(a,b,c):
----> 5     loesung1 = - (b/a)/2 + sqrt(((b/a)/2) * ((b/a)/2) - (c/a))
      6     loesung2 = - (b/a)/2 - sqrt(((b/a)/2) * ((b/a)/2) - (c/a))
      7 

TypeError: unsupported operand type(s) for /: 'str' and 'str'

提前致谢!!

【问题讨论】:

标签: python


【解决方案1】:

仅通过查看错误,变量abc 似乎作为字符串发送。然后您的 quadratgleichung 方法正在尝试划分字符串类型,这是它无法做到的。
在调用quadratgleichung
a = float(input("Enter a.")) 之前尝试将abc 转换为浮动,bc 也是如此

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多