【发布时间】: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