1. 程式人生 > >Python、循環的練習

Python、循環的練習

nbsp goto exit ora 16px tro -s pan span

from turtle import *
fillcolor("red")
begin_fill()
for i in range(5):
    up()
    goto(0,-20*i)
    down()
    circle(20*i)
end_fill()

一、同心圓

技術分享

二、太陽花

from turtle import *
color(red,yellow)
begin_fill()
while True:
    forward(200)
    right(150)
    if abs(pos())<1:
        break
end_fill()
done()

技術分享

三、五星紅旗



from turtle import *
setup(640,440,0,0)
color("yellow")
bgcolor("red")
fillcolor("yellow")
  
def llf_goto(x,y):
    up()
    goto(x,y)
    down()
     
def llf_draw(r):
    begin_fill()
    for i in range(5):         
        forward(r)
        right(144)
    end_fill()


llf_goto(-286,132)
llf_draw(
130) llf_goto(-132,180) llf_draw(50) llf_goto(-88,132) llf_draw(50) llf_goto(-88,72) llf_draw(50) llf_goto(-132,22) llf_draw(50)

 

四、畫菱形花

import turtle

def draw_1(llf):
    llf.forward(100)
    llf.right(45)
    llf.forward(100)
    llf.right(135)

def draw_2():
    window=turtle.Screen()
    window.bgcolor(
"blue") llf=turtle.Turtle() llf.shape("turtle") llf.color("orange") llf.speed("fastest") for i in range(1,37): draw_1(llf) draw_1(llf) llf.left(10) llf.right(90) llf.forward(155) llf.color(green) llf.forward(145) window.exitonclick() draw_2()

技術分享

五、根據學號取出年級、專業代號、學生編號

a = input("輸入學號:")
b = a[0:4]
c = a[4:9]
d = a[-3:]
print("年級是:{}".format(b))
print("專業代號是:{}".format(c))
print("學生編號是:{}".format(d))

技術分享

六、輸入身份證號檢測相關信息

import math
a = input("請輸入證件號碼(只能以440開頭):")
b = a[0:3]
if b == 440:
    print("你的證件號來源於廣東省!")
    if len(a)==18 :
        c = a[6:10]
        d = a[10:12]
        e = a[12:14]
        print("你的生日是:{}年{}月{}日".format(c,d,e))
        if a[3:6] == 100:
            print("你的直轄市為:廣州")
        elif a[3:6] == 103:
            print("你的市區為:荔灣區")
        elif a[3:6] == 104:
            print("你的市區為:越秀區")
        elif a[3:6] == 105:
            print("你的市區為:海珠區")
        elif a[3:6] == 106:
            print("你的市區為:天河區")
        f = a[16:17]
        f =int(f)
        if f % 2 != 0 :
            print("恭喜,你是男性!")
        else:
            print("賀喜,你不是男性!")
    else :
        print("你輸入的證件號有誤,請查證後再試!")
else :
    print("信息庫沒有改證件號的信息!")

技術分享

七、計算星期幾

a = input("請輸入數字(1——7)")
if int(a) == 1:
    print("今天是:星期一")
elif a == 2:
        print("今天是:星期二")
elif a == 3:
        print("今天是:星期三")
elif a == 4:
        print("今天是:星期四")
elif a == 5:
        print("今天是:星期五")
elif a == 6:
        print("今天是:星期六")
elif a == 7:
        print("今天是:星期日")

技術分享

Python、循環的練習