Python练手小栗子(不定时更新)


1.分秒转换器(统一转换为秒)
 主要使用到的函数:列表位置获取list.index(),新增字符串"".join()

 方法:将输入的字符串转化为列表,

1 timestr=input('请输入_小时_分钟_秒:例如\"5小时8分钟3秒\" >>>')
2 a=list(timestr)
3 hour="".join(a[:a.index('')])
4 minute="".join(a[a.index('')+1:a.index('')])
5 second="".join(a[a.index('')+1:a.index('')])
6 print('{}>>>>>{}秒'.format(timestr,3600*int(hour)+60*int(minute)+int(second)))

 。。。3.2版本中str更新了str.index()用法,故此法可进一步简化,学习Python还是看官方的实时文档好些

2.使用Python画一个太极图---turtle库练习

 1 from turtle import *
 2 #画布大小使用默认
 3 pu()
 4 goto(0,-300)
 5 pd()
 6 color('black','black')
 7 begin_fill()
 8 circle(150,180)
 9 circle(-150,180)
10 circle(-300,180)
11 end_fill()
12 circle(-300,180)
13 pu()
14 goto(0,-175)
15 pd()
16 color('black')
17 begin_fill()
18 circle(25)
19 end_fill()
20 pu()
21 goto(0,175)
22 pd()
23 color('white')
24 begin_fill()
25 circle(25)
26 end_fill()
27 hideturtle()#隐藏海龟图标
28 pu()
29 pencolor('black')
30 goto(0,-350)
31 write('太极',font=("微软雅黑", 12, "bold underline"))#设置字体
32 done()

3.沙漠之星-turtle库练习

 1 import turtle
 2 turtle.speed(12)
 3 turtle.color('black','red')
 4 turtle.begin_fill()
 5 for i in range(360):
 6     turtle.seth(i)
 7     for i in range(3):
 8         turtle.forward(150)
 9         turtle.left(120)
10 turtle.end_fill()
11 turtle.hideturtle()
12 turtle.pu()
13 turtle.goto(-20,-200)
14 turtle.write('沙漠之星',font=("微软雅黑", 12, "bold underline"))
15 turtle.done()

 4.神奇螺旋-turtle

 1 from turtle import *
 2 speed(12)
 3 i = 1
 4 colours=['black','white']
 5 while i<=300:
 6     width(3)
 7     forward(i)
 8     left(100)
 9     color(colours[i%2])
10     i += 1
11 hideturtle()
12 done()

相关