【问题标题】:unsupported operand type(s) for +: 'float' and 'str' help me [duplicate]+ 不支持的操作数类型:“float”和“str”帮助我[重复]
【发布时间】:2020-04-09 08:56:12
【问题描述】:

我是 python 新手 我想在一行中打印“12.6hellohello” 输入是'4.2 3 2 hello'

这是我的代码

import sys

words = []
for line in sys.stdin.readlines():
    words.append(line.strip())

e1 = input()
e2 = input()
e3 = input()
e4 = input()

e1 = float(e1)
e2 = int(e2)
e3 = int(e3)
word = e3*e4

print(round(e1*e2,1) + word)

我不明白为什么单独打印单词时,它可以工作但组合时不起作用

【问题讨论】:

    标签: python


    【解决方案1】:

    print 可以打印 float 如果它被传递给它,那不是问题。问题是,正如错误消息所说,floatstr 之间没有+ 运算符。解决此问题的一种方法是将float 显式转换为字符串:

    print(str(round(e1*e2,1)) + word)
    # Here^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多