IfElifElse Python(折扣方案)

2024-06-10 23:18:57 发布

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

我正在做一个程序,根据客户购买的数量给予折扣。

方向如下: 某些在线商店根据购买总额提供折扣:

        If the purchase total is over $50, a 5% discount is applied

        If the purchase total is over $100, a 10% discount is applied

        If the purchase total is over $500,  a 15% discount is applied

使用if elif else链计算应用任何折扣后的采购金额。对于您的演示,使用499.99美元的购买。

到目前为止,这是我创建的,但它似乎运行不正常,我想知道我能做些什么来改进我的代码,也许我能正确地使用if-elif-else代码。提前谢谢大家。

if total_cost>=10:    
   if give_shopper_5percent_discount():
     print"You have won a discount"
     total_cost -= discount
   candidate_prime =True

elif total_cost>100:
   if give_shopper_10percent_discount():
     print"You have won a discount"
     total_cost -= discount
   candidate_prime =True


else total_cost>=500:
   if give_shopper_15percent_discount():
     print"You have won a discount"
     total_cost -= discount
   candidate_prime =True

Tags: theifisdiscountpurchaseelseovertotal