TypeError不能将序列乘以'str'类型的非int

问题描述 投票:-3回答:1

我的程序抛出了这个错误:

File "C:\Users\...\Meal Price Calculator.py", 
line 6, in <module> meal = meal * tax 
TypeError: can't multiply sequence by  non-int of type 'str'

问题:

  • 我输错了符号吗?
  • 是标点符号吗?

我的代码:

meal = input('Enter Amount of Meal ')
tax = input('Enter tax percentage in decimal ')
tip = input('Enter tip percentage in decimal ')
meal = meal * tax
meal = meal / tip
print(meal)
input()
python windows
1个回答
1
投票

符号没有错。 这不是标点符号。 这是你的变量类型的问题。就像@Evert所说的那样,input()总是返回一个字符串。您必须手动将其转换为浮点或整数。 由于您要求输入为小数,因此您需要使用浮点数。 你的代码是这样的:

meal = float(input('Enter Amount of Meal '))
tax = float(input('Enter tax percentage in decimal '))
tip = float(input('Enter tip percentage in decimal '))
meal = meal * tax
meal = meal / tip
print(meal)

(PS。除非你双击脚本来运行它,否则我不会在最后看到输入点。我建议从命令行运行python脚本(cmd)。如果你需要帮助,请注释。 )

阅读有关变量here的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.