【问题标题】:I'm getting a type error and I'm not sure why我收到类型错误,我不知道为什么
【发布时间】:2017-11-09 04:01:50
【问题描述】:

我正在使用 Python 2.5。我不确定出了什么问题。我已经尝试过一百万种不同的方法,我很困惑。

这是我的代码:

import math

quad_function = "(a * x^2) + b * x + c"
print "A quadratic function is:" + quad_function

a_input = raw_input("Is 'a' given?")
b_input = raw_input("Is 'b' given?")
c_input = raw_input("Is 'c' given?")
x_input = raw_input("Are any of the solutions         given?")

if a_input == "yes":
    a = int(raw_input("What is 'a'?"))
if b_input == "yes":
    b = int(raw_input("What is 'b'?"))
if c_input == "yes":
    c = int(raw_input("What is 'c'?"))
if x_input == "one":
    x_1 = int(raw_input("What is the first     solution?"))
if x_input == "both":
    x_1 = int(raw_input("What is the first solution"))
x_2 = int(raw_input("What is the second solution"))

print 'The quadratic function is:' + str(a) + 'x^2'     + '+' + str(b) + 'x' + "+" + str(c)

d = b ** 2 - 4*a*c
if d > 1:
    num_roots = 2
if d == 1:
    num_roots = 1
if d < 1:
    num_roots = 0
print "There are " + str(num_roots) + " roots"

if x_input == "no" and d > 2 and a_input == 'yes' and b_input == 'yes' and c_input == 'yes':
x1 = float(((-b) + math.sqrt(d))/(2*a))
x2 = float(((-b) - math.sqrt(d))/(2*a))
print "The two solutions are: " + str(x1) * "and " + str(x2)

【问题讨论】:

  • 如果 a b c 没有给出,那么它们就没有定义吗?有了ayush的回答,这个问题可以归类/关闭为一个简单的印刷错误。
  • 可以在以下情况下关闭:此问题是由无法再复制的问题或简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者
  • 专业提示:这里欢迎家庭作业问题。但是,最好自信地问。不要喋喋不休或乞求,这就像要求投反对票一样。 “请阻止我”、“对我好一点”、“提前谢谢”都是最好避免的。无论如何,我们都喜欢简洁的问题。

标签: python typeerror traceback


【解决方案1】:

您在打算使用加号的地方使用了星号。替换

print "The two solutions are: " + str(x1) * "and " + str(x2)

print "The two solutions are: " + str(x1) + "and " + str(x2)

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 2017-11-23
    • 2019-02-14
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多