Python基础篇_实例练习1
Posted dmsj20190707
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础篇_实例练习1相关的知识,希望对你有一定的参考价值。
1、逢7跳过小游戏:从1-100之间,遇到带7的数字或者7的倍数跳过。
1 for i in range(1,101): 2 if i == 7 or i % 10 == 7 or i // 10 == 7: 3 continue 4 else: 5 print(i,end = ",")
2、七段数码管的绘制:模拟电子管显示屏显示日期
1 import turtle 2 import time 3 def drawGap(): 4 turtle.penup() 5 turtle.fd(5) 6 def drawLine(draw): 7 drawGap() 8 turtle.pendown() if draw else turtle.penup() 9 turtle.fd(40) 10 drawGap() 11 turtle.right(90) 12 #开始绘制 13 def drawDigit(digit): 14 drawLine(True) if digit in (2,3,4,5,6,8,9) else drawLine(False) 15 drawLine(True) if digit in (0,1,3,4,5,6,7,8,9) else drawLine(False) 16 drawLine(True) if digit in (0,2,3,5,6,8,9) else drawLine(False) 17 drawLine(True) if digit in (0,2,6,8,9) else drawLine(False) 18 turtle.left(90) 19 drawLine(True) if digit in (0,4,5,6,8,9) else drawLine(False) 20 drawLine(True) if digit in (0,2,3,4,5,6,7,8,9) else drawLine(False) 21 drawLine(True) if digit in (0,1,2,3,4,7,8,9) else drawLine(False) 22 turtle.left(180) 23 turtle.penup() 24 turtle.fd(20) 25 def drawDate(date): 26 turtle.pencolor("red") 27 for i in date: 28 if i == "-": 29 turtle.write(‘年‘,font=("Arial",18,"normal")) 30 turtle.pencolor("green") 31 turtle.fd(40) 32 elif i == "=": 33 turtle.write(‘月‘,font=("Arial",18,"normal")) 34 turtle.pencolor("green") 35 turtle.fd(40) 36 elif i == "+": 37 turtle.write(‘日‘,font=("Arial",18,"normal")) 38 else: 39 drawDigit(eval(i)) 40 def main(): 41 turtle.setup(800,350,200,200) 42 turtle.penup() 43 turtle.fd(-300) 44 turtle.pensize(5) 45 drawDate(time.strftime(‘%Y-%m=%d+‘,time.gmtime())) 46 turtle.hideturtle() 47 turtle.done() 48 main()
以上是关于Python基础篇_实例练习1的主要内容,如果未能解决你的问题,请参考以下文章