python1.输入红包金额和红包个数,程序要有提示信息。 2.然后选择红包分配方式,若选择平均分配,则按红包个数平均分配红包金额3.若选择随机分配,则按红包个数随机分配红包金额。
时间: 2023-08-06 19:08:22 浏览: 66
以下是实现红包分配的 Python 代码:
```python
import random
# 输入红包金额和红包个数
total_amount = float(input("请输入红包金额:"))
num = int(input("请输入红包个数:"))
# 选择红包分配方式
choice = input("请选择红包分配方式(输入1表示平均分配,输入2表示随机分配):")
if choice == "1": # 平均分配
amount = total_amount / num
print("每个红包的金额为:", amount)
elif choice == "2": # 随机分配
amounts = []
for i in range(num):
if i == num - 1:
amount = total_amount - sum(amounts)
else:
amount = random.uniform(0, total_amount - sum(amounts))
amounts.append(amount)
random.shuffle(amounts)
print("每个红包的金额为:", amounts)
else:
print("输入错误,请重新输入!")
```
这个程序首先让用户输入红包金额和红包个数,然后让用户选择红包分配方式。如果选择平均分配,程序就按照红包个数平均分配红包金额;如果选择随机分配,程序就按照红包个数随机分配红包金额。最后输出每个红包的金额。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)