我不明白如何在这些信件中输入金额?

i dont understand how can I put an amount in these letters?

totalBought = float(input("how much you bought in total:")) 
discountAmt = input("what is your status:")
if discountAmt == "S":
    S == 10
elif discountAmt == "O":
    O == 5
elif discountAmt == "M":
    M == 15
elif discountAmt == "E":
    E == 20

totalPrice = totalBought - discountAmt

print(totalPrice)

这些字母分别是折扣、学生、老人、残疾人、vips

在第二行,discountAmt 被分配了一个字符串值。该字符串值在整个代码中保持其值。所以当你做 totalBought - discountAtm 时,你做的是一个数字减去一个字符串,这是没有意义的。此外,您需要区分 ===。您可以做的是用 discoutAmt = 10discountAmt = 5 等替换所有 S == 10O == 5 等。或者,您可以为折扣金额指定一个单独的变量,如果您不想覆盖折扣代码。

首先,你使用的是==,其中returns是一个boolean,使用=来赋值一个变量。 其次,不需要创建不同的变量。只需使用一个 S 即可分配折扣。

参考下面的解决方法:

totalBought = float(input("how much you bought in total:")) 
discountAmt = input("what is your status:")

if discountAmt == "S":
    S = 10
elif discountAmt == "O":
    S = 5
elif discountAmt == "M":
    S = 15
elif discountAmt == "E":
    S = 20

totalPrice = totalBought - S
print(totalPrice)

您可以尝试使用词典:

使用字典代码可读性更强,if else 条件更少

totalBought = float(input("how much you bought in total:"))
discountDict ={
     'S':10,
     'O': 5,
     'M':15,
     'E':20

}
discountAmt = input("what is your status:")


totalPrice = totalBought - discountDict[discountAmt]

print(totalPrice)