在python中使用if-elif-else来创建折扣计算器

时间:2014-11-07 14:27:41

标签: python

我很抱歉我对此很新,这对我来说非常困惑。我搜索了问题,似乎没有我想要的答案。我很满意一切,直到它询问你买了多少袋。我希望该计划能够确定要应用的折扣率。然后我想要程序来计算mount值;要应用的折扣率和客户要支付的最终金额。这真让我感到沮丧,因为我不知道从哪里开始。我认为最好的方法是使用if-elif-else,但显然我可能是非常错误的。任何帮助都会受到重视。

print "Bags Discount Calculator"
print "Please follow he on screen instructions"
print "--------------------------------------- "
print "Rate of Discount based on the following:"
print "____________________________________________"
print "Number of Bags:            Rate of Discount:"
print "____________________________________________"
print "Less than 25                       0%"
print "From 25-49                         5%"
print "From 50-99                         10%"
print "100 or more                        20%"
print "_____________________________________________"
password=raw_input ("Please enter your password:")
if (password=="di8count"):
print ("\n\tPasssword accepted")
numberofbags=raw_input ("Enter number of bags purchasd:")

if (numberofbags<25):
     discountRate = .05




else:
    print("\n\tIncorrect Password. Please try again.")  

4 个答案:

答案 0 :(得分:1)

你想要做的是创建一个函数,比如说calculate_discount并让它为你工作。这样,当您想要在代码中的其他位置调用它时,它是可重用的。该功能将包的数量作为参数和产品的全价,并计算应用多少折扣并返回折扣价。这样的事情应该有效:

def calculate_discount(nbags, price):
    if nbags < 25:
        discount = 0
    elif 25 <= nbags < 50:
        discount = 0.05
    elif 50 <= nbags < 100:
        discount = 0.1
    else:
        discount = 0.2
amount_discounted = discount * price
final_price = price - amount_discounted
return final_price

答案 1 :(得分:1)

print "Bags Discount Calculator"
print "Please follow he on screen instructions"
print "--------------------------------------- "
print "Rate of Discount based on the following:"
print "____________________________________________"
print "Number of Bags:            Rate of Discount:"
print "____________________________________________"
print "Less than 25                       0%"
print "From 25-49                         5%"
print "From 50-99                         10%"
print "100 or more                        20%"
print "_____________________________________________"
password=raw_input ("Please enter your password:")
if (password=="di8count"):
    print ("\n\tPasssword accepted")
    numberofbags=input ("Enter number of bags purchasd:")


    def fun(dis):
        print "rate of discount : "+str(dis*100)+"%"
        print "price : "+str(numberofbags*5)
        print "discount received : "+str(dis*numberofbags*5)
        print "total amount due : "+str((numberofbags*5)-(dis*numberofbags*5))

    if (numberofbags<25):
        fun(0)
    elif (numberofbags<50):
        fun(0.05)
    elif (numberofbags<100):
        fun(0.1)
    else:
        fun(0.2)


else:
    print("\n\tIncorrect Password. Please try again.")

答案 2 :(得分:0)

print "Bags Discount Calculator"
print "Please follow he on screen instructions"
print "--------------------------------------- "
print "Rate of Discount based on the following:"
print "____________________________________________"
print "Number of Bags:            Rate of Discount:"
print "____________________________________________"
print "Less than 25                       0%"
print "From 25-49                         5%"
print "From 50-99                         10%"
print "100 or more                        20%"
print "_____________________________________________"
password=raw_input ("Please enter your password:")
if (password=="di8count"):
    print ("\n\tPasssword accepted")
    numberofbags=raw_input ("Enter number of bags purchasd:")

    if (numberofbags<25):
        discountRate = 0
    elif (numberofbags<50):
        discountRate = 0.05
    elif (numberofbags<100):
        discountRate = 0.1
    else:
        discountRate = 0.2
else:
    print("\n\tIncorrect Password. Please try again.")

答案 3 :(得分:0)

calculate_discount(nbags, price):
    if nbags < 25:
        discount = 0
    elif 25 <= nbags < 50:
        discount = 0.05
    elif 50 <= nbags < 100:
        discount = 0.1
    else:
        discount = 0.2
    amount_discounted = discount * price
    final_price = price - amount_discounted
    return final_price