首页 > 解决方案 > TypeError:不能将序列乘以“str”类型的非整数(我输入值 3、4、-2)

问题描述

TypeError:不能将序列乘以“str”类型的非整数(我输入值 3、4、-2)

def main():
    print("This program finds the real solutions to a quadratic")

    a, b, c = input("Please enter the coefficients (a, b, c): ").split(",")

    disc = float(pow(b*b - 4*a*c,0.5))
    
    root1 = float((-b + disc)/(2 * a))
    
    root2 = float((-b - disc)/(2 * a))

    print("The solutions are:" ,root1, root2)
                    
main()

标签: python

解决方案


input()方法从输入中读取一行,通过删除尾随换行符将该行转换为字符串,然后返回它。

因此,存储在 和 中的值ab字符串c。因此,它显示了您无法将字符串序列相乘的错误。

a, b, c = [int(x) for x in input("Please enter the coefficients (a, b, c): ").split(",")]

disc = float(pow(b*b - 4*a*c,0.5))

root1 = float((-b + disc)/(2 * a))

root2 = float((-b - disc)/(2 * a))

print("The solutions are:" ,root1, root2)

推荐阅读