总计=打印(小计+税+小费)类型错误:不支持的+操作数类型:'NoneType'和'NoneType'

2024-04-20 11:06:03 发布

您现在位置:Python中文网/ 问答频道 /正文


meal_cost = 10.00
tax_rate = 0.08
tip_rate = 0.20

#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
 
#When eating at a restaurant in the United States, it's
#customary to have two percentage-based surcharges added on
#top of your bill: sales tax and tip. These percentages are
#both applies to the original cost of the meal. For example,
#a 10.00 meal with 8% sales tax and 20% tip would add 0.80
#for tax (0.08 * 10.00) and 2.00 for tip (0.20 * 10.00).
#
#The variables above create the cost of a meal and identify
#what percentage should be charged for tax and tip.
#
#Add some code below that will print the "receipt" for a
#meal purchase. The receipt should look like this:
#
#Subtotal: 10.00
#Tax: 0.8
#Tip: 2.0
#Total: 12.8
#
#Subtotal is the original value of meal_cost, tax is the
#tax rate times the meal cost, the tip is the tip rate times
#the meal cost, and the total is the sum of all three numbers.
#Don't worry about the number of decimal places; it's fine
#if your code leaves off some numbers (like 0.8 for tax) or
#includes too many decimal places (like 2.121212121 for the tip).

 
#Add your code here!


Subtotal = print(float(meal_cost))
Tax= print(float(tax_rate*10))
Tip = print(float(tip_rate *100))
Total = print(Subtotal + Tax + Tip)
    Total = print(Subtotal + Tax + Tip) 
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

尽管我已经把所有的东西都转换成了float,但为什么我会得到这个错误呢

为什么添加float会导致这样的错误


Tags: andofthetoforyourratecode
2条回答

无法使用print()分配语句

另外,您的变量已经是float,您不需要将它们放在float()中

这样做:

Subtotal = meal_cost
Tax= tax_rate*10
Tip = tip_rate*100
Total = Subtotal + Tax + Tip
print(Total)

但是,如果要打印并同时在print()中指定变量(我不建议这样做),可以使用walrus运算符:

print(Subtotal := meal_cost)
print(Tax := tax_rate*10)
print(Tip := tip_rate*100)

print(Subtotal + Tax + Tip)
meal_cost = 10.00
tax_rate = 0.08
tip_rate = 0.20

#You may modify the lines of code above, but don't move them! #When you Submit your code, we'll change these lines to #assign different values to the variables.

#When eating at a restaurant in the United States, it's #customary to have two percentage-based surcharges added on #top of your bill: sales tax and tip. These percentages are #both applies to the original cost of the meal. For example, #a 10.00 meal with 8% sales tax and 20% tip would add 0.80 #for tax (0.08 * 10.00) and 2.00 for tip (0.20 * 10.00).

#The variables above create the cost of a meal and identify #what percentage should be charged for tax and tip.

#Add some code below that will print the "receipt" for a #meal purchase. The receipt should look like this:

#Subtotal: 10.00 #Tax: 0.8 #Tip: 2.0 #Total: 12.8

#Subtotal is the original value of meal_cost, tax is the #tax rate times the meal cost, tip is the tip rate times #the meal cost, and total is the sum of all three numbers. #Don't worry about the number of decimal places; it's fine #if your code leaves off some numbers (like 0.8 for tax) or #includes too many decimal places (like 2.121212121 for tip).

#Add your code here!

Subtotal = meal_cost            
Tax = tax_rate* meal_cost
Tip = tip_rate*meal_cost
Total = (Subtotal + Tax + Tip)
print("Subtotal:", Subtotal)
print("Tax:", Tax)
print("Tip:", Tip)
print("Total:", Total)

解决了它。感谢您的指导

相关问题 更多 >