1. 程式人生 > >條件、循環、函數定義、字符串操作練習9-14

條件、循環、函數定義、字符串操作練習9-14

cnblogs print input star 白雲 set form format for

用循環畫五角星

>>> import turtle
>>> for i in range(5):
turtle.forward(100)
turtle.right(144)
技術分享

用循環畫同心圓

>>> import turtle
>>> for i in range(4):
    turtle.penup()
    turtle.goto(0,-40*i)
    turtle.pendown()
    turtle.circle(40*i)
技術分享

用while循環畫太陽花

>>> from
turtle import* >>> color(red,blue) >>> begin_fill() >>> while True: forward(200) left(170) if (abs(pos()))<1: break >>> end_fill() >>> done()
技術分享

用函數定義畫五個五角星

>>>import turtle
>>>turtle.setup(600,400)

>>>turtle.color("
yellow") >>>turtle.bgcolor("red") >>>turtle.fillcolor("yellow") >>>turtle.speed(10) >>>def turtle_goto(x,y): turtle.up() turtle.goto(x,y) turtle.down() >>>def darw_star(d): turtle.begin_fill() for i in range(5): turtle.forward(d) turtle.right(
144) turtle.end_fill() >>>turtle_goto(-260,120) >>>darw_star(120) >>>turtle_goto(-110,160) >>>turtle.left(40) >>>darw_star(40) >>>turtle_goto(-65,125) >>>turtle.right(10) >>>darw_star(40) >>>turtle_goto(-55,55) >>>turtle.left(40) >>>darw_star(40) >>>turtle_goto(-110,15) >>>turtle.right(20) >>>darw_star(40)
技術分享

用函數定義畫鉆石花瓣的太陽花

>>>import turtle
>>>turtle.speed(30)
>>>turtle.color("green")
>>>turtle.fillcolor("blue")

>>>def draw_rhombus(size):
    for i in range(2):
        turtle.fd(size)
        turtle.left(20)
        turtle.fd(size)
        turtle.left(160)

>>>turtle.right(90)

>>>turtle.begin_fill()
>>>for i in range(36):
    draw_rhombus(100)
    turtle.left(10)
>>>turtle.end_fill()
>>>turtle.fd(400)
技術分享

輸入學號,識別年級、專業、序號

>>>name = input("請輸入名字:")
>>>num = input("請輸入學號:")

print("{}同學您好。".format(name))
print("您的年級為:{}級".format(num[0:4]))
>>>if(int(num[8:10])==43):
    print("您的班級為:網絡工程1班")
>>>elif(int(num[8:10])==44):
    print("您的班級為:網絡工程2班")
>>>elif(int(num[8:10])==41):
    print("您的班級為:軟件工程1班")
>>>elif(int(num[8:10])==42):
    print("您的班級為:軟件工程2班")
>>>print("您的序號為:{}".format(num[-2:]))
技術分享


輸入1-7的數字,輸出對應的“星期幾”

>>>s="星期一星期二星期三星期四星期五星期六星期天"
>>>a=int(input("請輸入(1-7):"))
>>>if(0<a<8):
    print(s[-3+3*a:0+3*a])
>>>else:
    print("輸入有誤。")
技術分享


識別身份證號中的省市區、年齡、性別

>>>ID=input(請輸入您的身份證號碼:)

>>>P=[廣東省,四川省]
>>>C=[廣州市,深圳市,成都市]
>>>R=[越秀區,白雲區,天河區,福田區,南山區,羅湖區]

>>>p=int(ID[0:2])
>>>c=int(ID[2:4])
>>>r=int(ID[4:6])
>>>sex=int(ID[16])
>>>age=int(ID[6:10])

>>>if p==44:
    print("您所屬的省:",P[0])
>>>elif p==51:
    print("您所屬的省:",P[1])
>>>else:
    print(查無數據。)

>>>if p==44:
    if c==1:
        print(C[0])   
>>>if r==4:
            print(D[0])
>>>elif r==11:
            print(D[1])
>>>elif  r==6:
            print(D[2])
>>>else:
             print(無數據。)
>>>elif c==3:
>>>if r==4:
            print(D[3])
>>>elif r==5:
            print(D[4])
>>>elif  r==3:
            print(D[5])
>>>else:
            print(無數據。)
>>>else:
        print(無數據。)

>>>if p==51:
>>>if c==1 and r==8:
        print(C[2],R[6])
>>>else:
        print(無數據。)

>>>if sex%2==0:
    print(女性)
>>>else:
    print(男性)

>>>age=2017-int(age)
>>>print(您今年{}歲.format(age))
技術分享


條件、循環、函數定義、字符串操作練習9-14