欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python基础篇_实例练习1

程序员文章站 2022-04-16 07:58:36
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、逢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()