If Elif Else Python(折扣场景)

If Elif Else Python(折扣场景),python,if-statement,Python,If Statement,我正在做这个计划,根据客户购买的数量提供折扣 方向如下: 某些在线商店根据购买的总金额提供折扣: 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 i

我正在做这个计划,根据客户购买的数量提供折扣

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

        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

你的压痕不正常。它在python中很重要。(试试这个?)


同样对于折扣,尝试乘以而不是减去

根据您的描述,第一个
总成本
限额是错误的。而且在总成本=500之前,您不应使用else

试试这个:

total_cost = int(input('Please enter a total_cost:'))
if total_cost>=500:    

   print"You have won a discount by 15 percent"
   total_cost *= 0.85



elif total_cost>100:

   print"You have won a discount by 10 percent"
   total_cost *= 0.9



elif total_cost>=50:

   print"You have won a discount by 5 percent"
   total_cost *= 0.95

else:
   print 'you total cost is not in the range of discount!'
print 'Now the total cost is ', total_cost

您必须先检查最大折扣,否则您将按预期提供更多折扣;-)


您需要修复缩进,它在Python中很重要,所以如果我们在您的问题中看不到它,我们就不知道您真正的代码是什么。您还需要准确地定义“似乎运行不正常”的含义,并提供
给予购物者5%折扣()
和朋友的定义,并通常显示一个人们可以运行以复制您的问题的程序。缩小问题点(python是否抛出错误?)并添加大量的打印语句以查看发生了什么。例如,我经常看到
total_cost-=折扣
,但折扣从未计算过,而且在每种情况下都应该不同。@PaulGriffiths。谢谢,我刚刚编辑过。我只是对这种格式是否是elif-else格式的工作方式感到困惑。我是否应该使用两个elifs函数并保存其他函数以用于其他对象?我很困惑,因为看起来我至少需要三个选项来使用它。如果total_cost>=10为false,那么total_cost>100怎么可能为true?我从未将任何设置为false。我不明白你的意思,我也漏掉了一行。在这里,我刚改成这个,但我一直得到一个错误。如果总成本>=10:如果给予购物者百分之五的折扣():打印“您赢得了折扣”总成本-=折扣候选成本\u prime=True elif总成本>100:如果给予购物者百分之十的折扣():打印“您赢得了折扣”总成本-=折扣候选成本=真否则总成本>=500:如果给购物者15%的折扣():打印“您赢得了折扣”总成本-=折扣候选成本=真折扣。我在堆栈溢出上使用缩进时遇到困难。但是我忘了在第一行加上如果总成本>=10:。但它告诉我,总成本是没有定义的。你有完整的代码吗?把它放在第一个帖子里。要缩进代码,请使用空格4。我刚试过,但是格式太差了。但是上面显示的代码是m170897017。我现在知道了如何使用else函数,但它似乎没有运行。这是说我的第一行有一个错误,没有定义总成本。我很困惑。我将总成本设置为折扣,对吗?如果没有,我需要定义三个不同的时间,因为有三个不同的折扣?谢谢。这就是我的想法。因此,如果没有折扣,我就使用else函数。但是,当我运行代码时,它表示第一行中没有定义总成本,我猜其他行也会有问题。帮助?我有三条总成本线,它们设置为折扣。我不明白。他们不会打折的
total_cost-=折扣
表示
total_cost=total_cost-折扣
@python2learn嗨,如果要使“total_cost-=折扣”生效,首先需要将值设置为total_cost。int(输入(输入total_cost))我遇到了问题。你是说我需要问他们一个问题吗?也许是我上面输入的代码?总成本已经有总成本和折扣正确吗?或者这能解决这个问题吗?总成本=总成本-折扣我认为这是主要问题。在if/elif/else测试中,通常要先测试最严格的条件。否则,您需要有更复杂的条件(如
if 10
total_cost = int(input('Please enter a total_cost:'))
if total_cost>=500:    

   print"You have won a discount by 15 percent"
   total_cost *= 0.85



elif total_cost>100:

   print"You have won a discount by 10 percent"
   total_cost *= 0.9



elif total_cost>=50:

   print"You have won a discount by 5 percent"
   total_cost *= 0.95

else:
   print 'you total cost is not in the range of discount!'
print 'Now the total cost is ', total_cost
if total_cost>=500:
  ...
elif total_cost>=100:
  ...
elif total_cost>=10:
  ...
else: 
  pass