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

为何代码出现TypeError: unsupported operand type(s) for //: 'str' and 'int'错误?

解决TypeError: unsupported operand type(s) for //: 'str' and 'int'错误

这个错误的核心原因是你尝试对字符串类型和整数类型执行整除(//)操作,Python不支持不同数值类型与字符串直接进行算术运算。

修复步骤

  • 定位代码中参与//运算的变量,确认哪一个是字符串类型,使用int()或float()将其转换为数值类型后再运算。
  • 示例修正:
    错误代码:
    rental_minutes = input("输入租赁分钟数: ")
    full_hours = rental_minutes // 60  # input返回字符串,与整数60运算触发错误
    print(f"租赁整小时数: {full_hours}")
    
    修正后代码:
    rental_minutes = int(input("输入租赁分钟数: "))
    full_hours = rental_minutes // 60
    print(f"租赁整小时数: {full_hours}")
    

报错代码场景参考

代码报错示例

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 10:41:31