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)
these letters are discounts ,students,old people,pwds,and vips
CodePudding user response:
On the second line, discountAmt is assigned a string value. That string value remains its value throughout the code. And so when you do totalBought - discountAtm, you do a number minus a string, which doesn't make sense. Also, you need to distinguish between == and =. What you can do is replace all those S == 10, O == 5, etc. with discoutAmt = 10, discountAmt = 5, etc. Or, you can dedicate a separate variable to the discount amount, if you don't want to override the discount code.
CodePudding user response:
First of all, you are using == which returns a boolean, use = to assign a variable.
Secondly, no need to create different variables. Just use one S to assign discounts.
Refer to the solution below:
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)
CodePudding user response:
You can try something with Using Dictionary:
Using Dictionary code is more more readable and less if else condition
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)
