You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Python温度转换函数传入列表时出现“can't multiply sequence by non-int of type 'float'”错误?

温度单位转换函数兼容列表输入的问题解决

问题描述

作为Python新手,编写了摄氏度(C)、华氏度(F)、开尔文(K)互转的函数,单个数值输入时正常,但传入列表[0,10,20]时触发错误:TypeError: can't multiply sequence by non-int of type 'float',尝试声明x的数值类型后仍未解决。

原代码

import warnings

def convTemp(x=0, fro="C", to="F"):
    if fro == "C" and to == "F":
        x = 1.8 * x + 32
        return x
    elif fro == "C" and to == "K":
        x = x + 273.15
        return x
    elif fro == "F" and to == "C":
        x = x * (5/9)-32
        return temp
    elif fro == "F" and to == "K":
        x = (x * (5/9)-32)+273.15
        return x
    elif fro == "K" and to == "C":
        x = x - 273.15
        return x
    elif fro == "K" and to == "F":
        x = (9/5)*(x - 273.15) + 32
        return x
    else:
        if fro == to:
            warnings.warn("Your 'fro' parameter is the same as your 'to' parameter!")

报错栈

TypeError                                 Traceback (most recent call last)
<ipython-input-2-e9e7ea7efd49> in <module>
      1 assert convTemp(0) == 32
----> 2 assert convTemp([0,10,20]) == [32, 50, 68]
      3 
<ipython-input-1-a8e50b49bfb6> in convTemp(x, fro, to)
      2 def convTemp(x=0, fro="C", to="F"):
      3     if fro == "C" and to == "F":
----> 4         x = 1.8 * x + 32
      5         return x
      6     elif fro == "C" and to == "K":

TypeError: can't multiply sequence by non-int of type 'float'

错误原因

Python中列表不能直接与浮点数相乘:整数乘列表会重复元素(如2 * [1,2]得到[1,2,1,2]),但浮点数与列表的乘法操作不被支持,因此传入列表时触发报错。此外原代码还存在两个逻辑错误:

  • 华氏度转摄氏度的公式错误,正确逻辑应为(x - 32) * 5/9,原代码写成了x * 5/9 -32
  • 华氏度转摄氏度分支中错误返回了未定义的变量temp,应为返回x

解决方案

修改函数,使其同时支持单个数值和列表(或其他可迭代对象)输入,同时修复公式错误:

import warnings
from collections.abc import Iterable

def convTemp(x=0, fro="C", to="F"):
    # 单个数值的转换逻辑
    def convert_single(val):
        if fro == "C" and to == "F":
            return 1.8 * val + 32
        elif fro == "C" and to == "K":
            return val + 273.15
        elif fro == "F" and to == "C":
            return (val - 32) * (5/9)  # 修复公式顺序
        elif fro == "F" and to == "K":
            return (val - 32) * (5/9) + 273.15  # 修复公式顺序
        elif fro == "K" and to == "C":
            return val - 273.15
        elif fro == "K" and to == "F":
            return (9/5)*(val - 273.15) + 32
        else:
            if fro == to:
                warnings.warn("Your 'fro' parameter is the same as your 'to' parameter!")
            return val  # 单位相同时返回原值
    
    # 判断输入是否为可迭代对象(排除字符串)
    if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
        return [convert_single(val) for val in x]
    # 单个数值输入的情况
    else:
        return convert_single(x)

测试验证

执行以下断言代码可验证功能正常:

assert convTemp(0) == 32.0
assert convTemp([0,10,20]) == [32.0, 50.0, 68.0]
assert convTemp(32, fro="F", to="C") == 0.0
assert convTemp([32, 50, 68], fro="F", to="C") == [0.0, 10.0, 20.0]

内容的提问来源于stack exchange,提问作者LeroyBrown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:35:13