用户:Python染辞查看:9 回复:2 评论:9 创建时间:2021-08-18T12:04:31
【作品展示】

【作品介绍】
分配红包
【作品源代码】
import random
List = []
List1 = []
number = 1
number1 = 0
# 输入参数
while True:
money = input('''输入红包金钱数:
''')
if len(money.split('.')[-1]) >= 3:
print('钱的单位不能小于分!')
continue
try:
money = float(money)
if money == 0.0:
print('钱数不能为0!')
continue
elif abs(money) != money:
print('钱数不能为负数!')
continue
else:
pass
break
except ValueError:
print('请输入纯数字!')
while True:
people = input('''输入分配人数:
''')
try:
people = float(people)
if int(people) != people:
print('请输入正整数!不能分割人!!')
elif abs(people) != people:
print('请输入正整数!')
elif people == 0.0:
print('人数不能为0!')
else:
people = int(people)
break
except ValueError:
print('请输入正整数!')
while True:
model = input('''请输入分配红包模式(平均分配 & 随机分配):
''')
if model == '平均分配':
break
elif model == '随机分配':
break
else:
print('请输入上面两个模式')
# 开始分配
List2 = str(money).split('.')
if money == float(people):
print('你的钱数和人数相同,为你切换到平均分配模式')
model = '平均分配'
print('——————————————————————')
print('金钱为:' + str(money))
print('人数为:' + str(people))
print('模式为:' + model)
print('开始分配')
print('——————————————————————')
money1 = money
if model == '随机分配':
for i in range(people):
List1.append(0)
for i in range(int(List2[0])):
number1 = random.randint(0, people - 1)
List1[number1] += 1
for i in range(int(List2[1])):
number1 = random.randint(0, people - 1)
if len(str(List2[1])) == 1:
List1[number1] += 0.1
if len(str(List2[1])) == 2:
List1[number1] += 0.01
for i in range(len(List1)):
if int(str(List1[i]).split('.')[-1]) >= 3:
List1[i] = round(List1[i],3)
print(List1)
elif model == '平均分配':
for i in range(people):
List1.append(0)
for x in range(len(List1)):
List1[x] += money / people
print(List1)
else:
pass
# 输出结果
for i in range(len(List1)):
print('第' + str(number) + '位分得' + str(List1[number - 1]) + '元')
number += 1
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn来看看我的贴子吧imgsrc="https://static.cod喵/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%BF%BB%E7%99%BD%E7%9C%BC.gif"alt="emotion_编程猫_翻白眼"
点赞0
评论