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
相关产品推荐
相关产品推荐

