这些是我学习python的例子,从入门到最后哦
hello,world,我来了
1 |
print('hello') |
员工管理系统
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
print('-'*20,'欢迎进入员工管理系统','-'*20) emps=['孙悟空\t18\t男\t花果山','许文龙\t30\t男\t高老庄','叶陈\t47\t女\t流沙河']#这里一个员工的基本信息包括姓名,年龄,性别,住址,且保存方式为放到一个字符串中。 while True : print('-'*66) print('\t1.查询员工信息') print('\t2.添加员工信息') print('\t3.删除员工信息') print('\t4.退出管理系统') print('-'*66) choose = input('请输入您的操作') if choose == '1' : #input返回的值是字符串,所有此处应该将1改成字符串 #查询员工信息 print('\t序号\t姓名\t年龄\t性别\t地址') n=1 #创建一个序号,初始值为1 for emp in emps : print(f'\t{n}\t{emp}') #因为员工的所有信息是一个字符串,用列表保存,列表里面的每一个元素都是以字符串形式存储,则输出emp则会将员工的所有信息输出 n+=1 elif choose == '2' : #添加员工信息 emp_name=input('请输入员工的名字') emp_age=input('请输入员工的年龄') emp_sex=input('请输入员工的性别') emp_address=input('请输入员工的住址') emp=f'{emp_name}\t{emp_age}\t{emp_sex}\t{emp_address}' #一定要记住员工的所有信息就是列表中的一个元素 #判断用户输入是否确定流程 choose_a=input('是否确定操作?[Y/N]') if choose_a == 'y' or choose_a == 'yes' : emps.append(emp) print('添加成功!') else : print('添加失败!') elif choose == '3' : choose_name=int(input('请输入删除员工的序号')) # 此处注意,下面的choose_name需要减1,所以必须将其转换为int类型 print('以下员工信息将被删除') print(emps[choose_name-1]) choose_name_a=input('是否确定操作[Y/N]') if choose_name_a =='y' or choose_name_a =='yes' : emps.pop(choose_name-1) #此处,pop()是一个方法,参数是int类型,表示要删除序列的索引 else : print('删除失败') elif choose == '4' : input('请按enter键,退出员工管理系统') break else : #用户输入了除1-4以外的操作 print('-' * 66) print('您的操作有误请重新输入') |
唐僧大战白骨精
这个小游戏真的很无聊,大佬别喷,,,那时我才学到input和print。。==
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#分割线 print('-'*66) print('-'*20,'欢迎来到<-唐僧大战白骨精->','-'*20) print('请选择你的游戏身份:') print('\t1.唐僧') print('\t2.白骨精') choose_game=input() if choose_game=='1':#注意input返回的值是字符串类型,所有此处应该是字符串1,而不是整数1! #分割 print('-'*66) print('游戏即将开始,你将以<-唐僧->的身份进行游戏!') elif choose_game=='2': #分割线 print('-'*66) print('你居然选择白骨精,太不要脸了!系统自动为你分配<-唐僧->') else : #分割线 print('-'*66) print('你的输入有误,系统将自动为你分配角色!<-唐僧->') life=2 #你的初始生命值 attack=2 #你的初始攻击力 boss_life=10 #boss的初始生命值 boss_attack=10 #boss的初始攻击力 print(f'你的生命值为{life} ,你的攻击力为{attack}') while True : print('请选择你的操作:') print('\t1.练级') print('\t2.打boss') print('\t3.逃跑') choos_game2=input() if choos_game2 == '1' : life+=2 attack+=2 print(f'恭喜你升级了!你现在的生命值是{life},你的攻击力为{attack}') print('-'*66) #分割线 elif choos_game2 == '2' : print('唐僧攻击了白骨精!') print('白骨精反击了唐僧!') if boss_life <= attack : print(f'唐僧的攻击力为{life},白骨精的生命值为{boss_life},白骨精被杀死!') print('恭喜你取得了胜利') break else : print('你被白骨精杀死了!游戏结束!') break else : print('你输入的有误,请重新输入!') print('-'*66) input('请按任意键退出游戏!') |
文本进度条
主要学习格式化输出
1 2 3 4 5 6 7 8 9 10 11 12 |
import time scale=50 print("执行开始".center(scale//2,"-")) start=time.perf_counter() for i in range(scale+1): a ='*'*i b= '.'*(scale-i) c=(i/scale)*100 dur=time.perf_counter()-start print("\r{:3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='') time.sleep(0.1) print("\n"+"执行结束".center(scale//2,'-')) |
汉诺塔问题
主要学习函数和递归
1 2 3 4 5 6 7 8 9 10 11 12 13 |
count=0 def hannoi(n ,src ,dst, mid): global count if n == 1: print("{}:{}->{}".format(1,src,dst)) count+=1 else : hannoi(n-1, src, mid, dst) print("{}:{}->{}".format(n,src,dst)) count+=1 hannoi(n-1, mid, dst, src) hannoi(20,'A', 'B', 'C') print(count) |
计算圆周率
通过随机函数生成,概论性求解Π的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import random import time text=1000*1000 hit=0.0 start=time.perf_counter() for i in range(1 , 1+text): x=random.random();y=random.random() dist=pow(x*x+y*y,0.5) if dist <=1.0: hit=hit+1.0 pi=4*(hit/text) end=time.perf_counter()-start print("圆周率的值是{:.20f}".format(pi)) print("程序运行的时间是{}".format(end)) |
玫瑰花的绘制
主要学习turtle库
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
#RoseDraw.py import turtle as t # 定义一个曲线绘制函数 def DegreeCurve(n, r, d=1): for i in range(n): t.left(d) t.circle(r, abs(d)) # 初始位置设定 s = 0.2 # size t.setup(450*5*s, 750*5*s) t.pencolor("black") t.fillcolor("red") t.speed(100) t.penup() t.goto(0, 900*s) t.pendown() # 绘制花朵形状 t.begin_fill() t.circle(200*s,30) DegreeCurve(60, 50*s) t.circle(200*s,30) DegreeCurve(4, 100*s) t.circle(200*s,50) DegreeCurve(50, 50*s) t.circle(350*s,65) DegreeCurve(40, 70*s) t.circle(150*s,50) DegreeCurve(20, 50*s, -1) t.circle(400*s,60) DegreeCurve(18, 50*s) t.fd(250*s) t.right(150) t.circle(-500*s,12) t.left(140) t.circle(550*s,110) t.left(27) t.circle(650*s,100) t.left(130) t.circle(-300*s,20) t.right(123) t.circle(220*s,57) t.end_fill() # 绘制花枝形状 t.left(120) t.fd(280*s) t.left(115) t.circle(300*s,33) t.left(180) t.circle(-300*s,33) DegreeCurve(70, 225*s, -1) t.circle(350*s,104) t.left(90) t.circle(200*s,105) t.circle(-500*s,63) t.penup() t.goto(170*s,-30*s) t.pendown() t.left(160) DegreeCurve(20, 2500*s) DegreeCurve(220, 250*s, -1) # 绘制一个绿色叶子 t.fillcolor('green') t.penup() t.goto(670*s,-180*s) t.pendown() t.right(140) t.begin_fill() t.circle(300*s,120) t.left(60) t.circle(300*s,120) t.end_fill() t.penup() t.goto(180*s,-550*s) t.pendown() t.right(85) t.circle(600*s,40) # 绘制另一个绿色叶子 t.penup() t.goto(-150*s,-1000*s) t.pendown() t.begin_fill() t.rt(120) t.circle(300*s,115) t.left(75) t.circle(300*s,100) t.end_fill() t.penup() t.goto(430*s,-1070*s) t.pendown() t.right(30) t.circle(-600*s,35) t.done() |
文件读入写入
1 2 3 4 5 |
f=open('china.txt','at',encoding='UTF-8') # s=s.read()不读取了,现在写入 s=['小红','小明','大龙'] #这里有报错了,是因为之前的f那里的变量,我是只赋值给它r的功能,并不能写入,看来学这个必须得认真 f.write(' '.join(s)) |
生成词云库
利用文件读入,我读入网上的三封一千字左右的情书。。。。然后通过wordcloud库生成了词云,一起来看看情书告白里面,哪个词用的最多,嘿嘿~
1 2 3 4 5 6 7 8 9 |
import jieba import wordcloud f=open('love.txt', 'rt', encoding='UTF-8') txt=f.read() s=jieba.lcut(txt) print(s) w=wordcloud.WordCloud(width=1000, height=700, font_path="C:\\Windows\\Fonts\\方正粗黑宋简体.ttf", background_color='white') w.generate(" ".join(s)) w.to_file('pycloud.png') |
emmmmm,思念,我们,知道,用的最多呢