【问题标题】:TypeError: unsupported operand typ [duplicate]TypeError:不支持的操作数类型 [重复]
【发布时间】:2020-01-23 14:42:07
【问题描述】:

我刚接触 python,正在尝试学习这种语言。我尝试在 python 中运行代码但出现错误任何人都可以帮助我 这是我的代码:

def cel_to_fahr(c):
    f = c * 9/5 + 32
    return f

c = input("enter the temperature in celcius:")
print(cel_to_fahr(c))

我得到的错误是:

File ".\prg.py", line 6, in <module>
    print(cel_to_fahr(c))
  File ".\prg.py", line 2, in cel_to_fahr
    f = c * 9/5 + 32
TypeError: unsupported operand type(s) for /: 'str' and 'int

'

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    函数input() 返回字符串值。所以将其转换为intfloat

    c = int(input("enter the temperature in celcius:"))
    

    c = float(input("enter the temperature in celcius:"))
    

    【讨论】:

      【解决方案2】:

      您首先需要将输入转换为数字。 input()函数返回字符串值

      c = input("enter the temperature in celcius:")
      print(cel_to_fahr(int(c)))
      

      【讨论】:

        猜你喜欢
        • 2016-10-15
        • 2020-02-29
        • 2011-03-08
        • 2016-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多