我的程序抛出了这个错误:
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()
符号没有错。
这不是标点符号。
这是你的变量类型的问题。就像@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的更多信息