【问题标题】:add floating point and integer and take absolute value浮点数和整数相加并取绝对值
【发布时间】:2018-09-22 00:55:30
【问题描述】:

您好,我对代码很陌生,我需要知道如何输出通过计算 |x+y| 获得的值其中 x 是浮点数,y 是整数,都是未知输入。我尝试了很多事情,但都无法成功,请帮助我。

这就是我现在所拥有的,但我知道这是非常错误的 :( 在大多数情况下,当我尝试事情时,我会收到错误 TypeError: unsupported operand type(s) for +: 'int' and 'str' 这是我最近的尝试:

x = int(input())

y = input()

w = (x + y)

print (abs(w))

【问题讨论】:

    标签: python floating-point integer addition absolute-value


    【解决方案1】:

    您需要将y 转换为float

    x = int(input())
    y = float(input())
    w = (x + y)
    print(abs(w))
    

    函数input 返回一个字符串,来自文档:

    如果提示参数存在,则将其写入标准输出 没有尾随换行符。然后该函数从输入中读取一行, 将其转换为字符串

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 2018-07-16
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多