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

Python代码提交学习平台遇类型错误:需将int转为str求助

问题:返回类型不匹配(int/str)

我是编程初学者,在一个需要完成1-18题才能进入下一题的学习平台提交Python代码时遇到错误。代码在Colab里运行正常,但平台提示返回类型错误:返回类型为int,预期类型为str。我试了两种写法,一种用max()和.index方法得到索引3,另一种直接输出max值101,都触发了这个错误,求解决。

尝试的代码1

train_speeds = [64, 76, 93, 101, 84]

fastest_speed = max(train_speeds)

fastest_speed = train_speeds.index(fastest_speed)

print(fastest_speed)

输出结果:3

尝试的代码2

train_speeds = [64, 76, 93, 101, 84]

print(max(train_speeds))

输出结果:101

错误信息

Your result was in the wrong type

  • You returned type: int
  • Expected type: str

解决方案

平台要求返回字符串类型,但你输出的是整数。只需将最终结果转换成字符串再输出即可:

如果需要返回最大值(101)

train_speeds = [64, 76, 93, 101, 84]
print(str(max(train_speeds)))

如果需要返回最大值的索引(3)

train_speeds = [64, 76, 93, 101, 84]
fastest_speed = max(train_speeds)
index = train_speeds.index(fastest_speed)
print(str(index))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 20:01:11