python - 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()
解决方案
该input()
方法从输入中读取一行,通过删除尾随换行符将该行转换为字符串,然后返回它。
因此,存储在 和 中的值a
是b
字符串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)
推荐阅读
- pointers - 在 go 中包裹在括号中的指针尊重
- reactjs - 来自另一个包的钩子(useLocation...)
- javascript - 未调用 Miragejs 种子函数
- c++ - 编译并运行后输出未出现在 Visual Studio 代码中
- jmeter - 如何在 JMeter 函数中执行算术运算
- c# - 识别字符串是否与车牌格式相同[葡萄牙]
- javascript - 如何按消息日期和用户将消息分组
- triggers - MQ v9 - 自动启动触发器监视器并且没有 mqm 用户
- list - 如何将数据从屏幕保存到分离的类和另一个屏幕中的视图?
- sql - 在 Oracle 数据库中选择 CONCAT