【问题标题】:If-Elif-Else Python (Discount Scenario)If-Elif-Else Python(打折场景)
【发布时间】:2014-09-25 14:02:39
【问题描述】:

我正在开发这个计划,该计划根据客户的购买量提供折扣。

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

        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 中很重要,所以如果我们在您的问题中看不到它,我们不知道您的真实代码是什么。您还需要准确定义“似乎无法正常运行”的含义,并提供give_shopper_5percent_discount() 和朋友的定义,并通常展示人们可以运行以复制您的问题的程序。
  • 缩小问题点(python 是否抛出错误?)并添加大量打印语句以查看发生了什么。例如,我经常看到total_cost -= discount,但从不计算折扣,并且在每种情况下都应该不同。
  • @PaulGriffiths。谢谢我刚刚编辑了它。我只是对这种格式是否是 if-elif-else 格式的工作方式感到困惑。我应该使用两个 elifs 函数并将其他函数保存为其他函数吗?我很困惑,因为看起来我至少需要三个选项才能使用它。
  • 如果total_cost>=10为假,total_cost>100怎么可能为真?
  • 我从未将任何内容设置为 false。我不明白你的意思

标签: python if-statement


【解决方案1】:

根据您的描述,第一个total_cost 限制是错误的。并且你不应该在total_cost >= 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

【讨论】:

  • 谢谢。这就是当时的想法。所以如果没有折扣,我会使用 else 函数。但是,当我运行代码时,它说总成本未在第一行中定义,我猜这对其他行也会有问题。
  • 帮助?我有三个 total_cost 行,它们设置为折扣。我不明白。
  • 它们没有设置为折扣。 total_cost -= discount 表示total_cost = total_cost - discount
  • @python2learn 你好,你需要先给total_cost设置一个值,这样'total_cost -= discount'才会生效。
  • int(input(Enter total_cost)) 我遇到了麻烦。你是说我需要问他们一个问题吗?也许我在上面输入的代码?总成本已经有总成本和折扣正确吗?或者这会解决这个问题吗?total_cost = total_cost - discount
【解决方案2】:

你必须先检查最大的折扣,否则你会按预期给予更多的折扣;-)

if total_cost>=500:
  ...
elif total_cost>=100:
  ...
elif total_cost>=10:
  ...
else: 
  pass

【讨论】:

  • 我认为这是主要问题。在 if/elif/else 测试中,您通常希望首先测试最严格的条件。否则你需要有更复杂的条件(比如if 10 <= total_cost < 100)。
【解决方案3】:

你的缩进不正常。这在 python 中很重要。 (试试这个?)

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

对于折扣也可以尝试乘法而不是减法。

【讨论】:

  • 我也漏掉了一行。在这里,我刚刚更改为这个,但我不断收到错误消息。 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
  • 射击。我在堆栈溢出时使用缩进有困难。但我忘了在第一行添加 if total_cost>=10: 。但它向我展示了 total_cost 没有定义。
  • 你有完整的代码吗?将其发布在第一个帖子中。缩进代码,4个空格
  • 呃。我刚刚尝试过,但格式正在杀死我。但这是 m170897017 上面显示的代码。我现在知道如何使用 else 函数,但它似乎没有运行。这是说我的第一行有一个错误,其中未定义 total_cost。
  • 我很困惑。我将总成本设置为折扣,对吗?如果不是,我需要定义三个不同的时间,因为有三个不同的折扣?