python 微信红包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | def redbags(money, num = 10 ): import random choice = random.sample( range ( 1 , money * 100 ), num - 1 ) choice.extend([ 0 ,money * 100 ]) choice.sort() return [(choice[i + 1 ] - choice[i]) / 100 for i in range (num)] while True : money = input ( '请输入你要发放的红包金额:' ).strip() num = input ( '请输入你要放的红包数量:' ) if money.isdigit() and num.isdigit(): money = int (money) num = int (num) if money < = 0 or num < = 0 : print ( '输入金额或数量不能小于0' ) continue redbag = redbags(money,num) Max = max (redbag) nums = len (redbag) + 1 for index,p in enumerate (redbag, 1 ): print ( '第%s个人获得红包%s元' % (index,p)) if num = = index: break else : print ( '输入不合法' ) continue |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import random from time import sleep # 所有涉及金额的浮点数都需要用 round 方法保留2位小数,避免出现最终结果多出0.01 amount = round ( float ( input ( '请设置红包的金额 \> ' )), 2 ) num = int ( input ( '请设置红包的数量 \> ' )) hb_dict = {} xing = '赵钱孙李周吴郑王' ming = '一二三四五六七八九十' while num: xingming = random.choice(xing) + random.choice(ming) + random.choice(ming) if xingming in hb_dict.keys(): xingming = random.choice(xing) + random.choice(ming) + random.choice(ming) num - = 1 if num = = 0 : print ( '%s抢到红包%.2f元 红包抢完了!' % (xingming, amount)) hb_dict[amount] = xingming amount - = amount elif num > 0 : hb = round (random.uniform( 0.01 , amount) / num, 2 ) hb_dict[hb] = xingming # 算法: 在0.01到红包总金额之间随机一个浮点数 / 红包剩余个数 print ( '%s抢到红包%.2f元 剩余%d个!' % (xingming, hb, num)) amount = round ((amount - hb), 2 ) sleep( 1 ) # 转置字典中的 key / value # hb_dict2 = {value:key for key,value in hb_dict.items()} max_hb = max (hb_dict.items()) print ( '%s运气最佳 抢得%.2f元!!' % (max_hb[ 1 ], max_hb[ 0 ])) |
分类:
Python运维自动化
标签:
python自动化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具