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

Python input返回值相乘报TypeError: can't multiply sequence报错求解

问题原因

  • input()函数的返回值默认是字符串类型,你代码中采集的单份盎司数(e1o/e2o/e3o)、总供应份数serve都没有转换为数值类型,直接对两个字符串执行乘法运算就会触发你收到的TypeError。
  • 第二行打印语句中存在变量名拼写错误,你把e2o错写为e20,代码运行到这一步会额外触发NameError。

修正后代码

e1 = input("Enter Ingredient 1: ")
# 盎司数转浮点型,支持小数单位输入
e1o = float(input("How Many Ounces Of " + e1 + ':'))
e2 = input("Enter Ingredient 2: ")
e2o = float(input("How Many Ounces of " + e2 + ':'))
e3 = input("Enter Ingredient 3: ")
e3o = float(input("How many ounces of " + e3 + ':'))
# 供应份数转整型
serve = int(input('How Many Servings?: '))

print('Total ounces of ' + e1 + ': ' + str(serve * e1o))
print('Total ounces of ' + e2 + ': ' + str(serve * e2o))
print('Total ounces of ' + e3 + ': ' + str(serve * e3o))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 22:24:04