用Python编写简单的发红包程序和计算器原理
用Python编写简单的发红包程序:
第一种解法:数轴方法解决
import random
def red_packet(money,num):
money = money * 100 #将钱数转换成分为单位
ret = random.sample(range(1,money),num-1) #在最低钱数1分与总钱数之间生成人数减1个数作为数轴的节点
ret.sort() #对列表进行排序
ret.insert(0,0)
ret.append(money)
for i in range(len(ret)-1):
yield (ret[i+1]-ret[i])/100
ret_g = red_packet(100,10)
for money in ret_g:
print(money)
第二种解法:用概率解决
def red_packet(money,person):
dic_person_money = {}
for i in range(person):
num = random.randint(1,100)
dic_person_money['Person%s' % (i+1)] = num
num_sum = 0
for i in dic_person_money:
num_sum += dic_person_money[i]
for i in dic_person_money:
x = round(dic_person_money[i]/num_sum*money,2)
dic_person_money[i] = '$%s' % x
return dic_person_money
result = red_packet(1,10)
print(result)
用Python设计一个简单的计算器:
import re
def atom_cal(exp):
if '*' in exp:
a,b = exp.split('*')
return str(float(a)*float(b))
elif '/' in exp:
a, b = exp.split('/')
return str(float(a) / float(b)) def format_exp(exp):
exp = exp.replace('--','+')
exp = exp.replace('+-', '-')
exp = exp.replace('-+', '-')
exp = exp.replace('++', '+')
return exp def mul_div(exp):
while 1:
ret = re.search('\d+(\.\d+)?[*/]-?\d+(\.\d+)?',exp)
if ret:
atom_exp = ret.group()
res = atom_cal(atom_exp)
exp = exp.replace(atom_exp,res)
else:return exp def add_sub(exp):
ret = re.findall('[+-]?\d+(?:\.\d+)?',exp)
sum = 0
for i in ret:
sum += float(i)
return sum def cal(exp):
exp = mul_div(exp)
exp = format_exp(exp)
return add_sub(exp) def main(exp):
exp = exp.replace(' ','')
while 1:
ret = re.search('\([^()]+\)',exp)
if ret:
cal_exp = ret.group()
res = str(cal(cal_exp))
exp = exp.replace(cal_exp,res)
else:break
return cal(exp)
s = '1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
print(main(s))
用Python编写简单的发红包程序和计算器原理的更多相关文章
- python+redis简单实现发红包程序
redis是什么? Redis 是一个高性能的key-value数据库! 想进一步了解请移步搜索引擎自行查找. 编写这个小程序的目的就是对redis进行一个简单的小操作,对redis有一个初步的了解, ...
- 使用PHP编写发红包程序
使用PHP编写发红包程序 http://www.jb51.net/article/69815.htm 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-07-22 微信发红 ...
- VC++编写简单串口上位机程序
VC++编写简单串口上位机程序 转载: http://blog.sina.com.cn/s/articlelist_1809084904_0_1.html VC++编写简单串口上位机程序 串口通信 ...
- 使用Python编写简单的端口扫描器的实例分享【转】
转自 使用Python编写简单的端口扫描器的实例分享_python_脚本之家 http://www.jb51.net/article/76630.htm -*- coding:utf8 -*- #!/ ...
- Python 利用Python编写简单网络爬虫实例3
利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...
- Python 利用Python编写简单网络爬虫实例2
利用Python编写简单网络爬虫实例2 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://www.51testing. ...
- 编写简单的spring mvc程序,在tomcat上部署
编写简单的spring mvc程序,在tomcat上部署 1 用java 配置spring mvc ,可以省去web.xmlpackage hello;import org.springframewo ...
- python编写简单的html登陆页面(4)
python编写简单的html登陆页面(4) 1 在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将动态态分配数据,建立表格,存放学生信息 2 实现的效果如下: 3 动 ...
- python编写简单的html登陆页面(3)
1 在python编写简单的html登陆页面(2)的基础上在延伸一下: 可以将静态分配数据,建立表格,存放学生信息 2 加载到静态数据 3 html的编写直接在表格里添加一组数据就行了 4 V ...
随机推荐
- Spring MVC-从零开始-view-直接返回页面不传data
1.applicationContext配置 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...
- Python奇技淫巧 - 持续更新中....
Python奇技淫巧 人生苦短,我用Python: 编程界这绝对不是一句空话,尤其是对于使用过多个语言进行工作的同学们来说,用Python的时间越长,越有一种我早干嘛去了的想法,没事,啥时候用Pyth ...
- layui-table与layui-rate评分转换成星级的使用
需求:将layui-table中的某一列,例如:评分,从数据库中查找出来之后,进行layui-rate评分转换显示效果,为星星.显示效果如下: 实现代码: 1.layui中引入rate 2.table ...
- Angular toastr提示框
1. 安装ngx-toastr包 npm install ngx-toastr --save 2. package.json中引入toastr样式文件 "styles": [&qu ...
- sklearn 标准化数据的方法
Sklearn 标准化数据 from __future__ import print_function from sklearn import preprocessing import numpy a ...
- yii2 验证规则使用方法
required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...
- 本次作业统一标题:C语言I博客作业02
这个作业属于哪个课程 C语言程序设计1 这作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/8655 我在这个课程的目标是 ...
- webpack loader实现
正值前端组件化开发时代,那么必然离不开目前最火的构建工具--webpack(grunt,gulp等暂且不谈).说到这里,刚好有几个问题: 为什么运行打包命令之后,.vue 文件可以转成 .js 文件 ...
- spring5 源码深度解析----- Spring事务 是怎么通过AOP实现的?(100%理解Spring事务)
此篇文章需要有SpringAOP基础,知道AOP底层原理可以更好的理解Spring的事务处理. 自定义标签 对于Spring中事务功能的代码分析,我们首先从配置文件开始人手,在配置文件中有这样一个配置 ...
- request.getAttribute()和request.getParameter()
request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,request.setAttribute()和getAttribute()只是在web容 ...