python商场促销
时间: 2023-06-21 08:09:49 浏览: 85
好的,让我们来设计一个Python商场促销程序。以下是一个简单的示例代码:
```python
# 定义商品价格
prices = {
"apple": 2.5,
"banana": 1.5,
"orange": 3.0,
"grape": 4.0,
"watermelon": 5.0
}
# 计算总价
def calculate_total(items):
total = 0
for item in items:
if item in prices:
total += prices[item]
return total
# 计算折扣
def calculate_discount(total):
if total >= 50:
return 0.1 * total
elif total >= 30:
return 0.05 * total
else:
return 0
# 主函数
def main():
items = ["apple", "banana", "orange", "grape"]
total = calculate_total(items)
discount = calculate_discount(total)
final_total = total - discount
print("原价:%.2f元,折扣:%.2f元,总价:%.2f元" % (total, discount, final_total))
if __name__ == "__main__":
main()
```
在这个示例代码中,我们首先定义了一个字典 `prices`,包含各种商品的价格。然后我们定义了两个函数,`calculate_total` 用于计算购物车内商品总价,`calculate_discount` 用于根据总价计算折扣。最后我们在主函数中调用这两个函数,并打印出原价、折扣和最终总价。
你可以根据自己的需要修改商品价格和折扣策略,来实现不同的商场促销方案。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)