1. 程式人生 > >課堂練習(條件、循環、函數定義、字符串操作)

課堂練習(條件、循環、函數定義、字符串操作)

身份證 星期 rtl orm range 字符串操作 -1 mat card

畫五角星

import turtle for i in range(5): turtle.forward(200) turtle.right(144)

 技術分享

畫同心圓

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

 技術分享 

畫太陽花

import turtle
turtle.color(‘yellow‘,‘red‘)
turtle.speed(‘fastest‘)
turtle.begin_fill()
while True:
    turtle.forward(150)
    turtle.left(200)
    if abs(turtle.pos())<1:
        break
turtle.end_fill()
turtle.done()

技術分享

畫五星紅旗

import turtle turtle.setup(600,400,0,0) turtle.color("yellow") turtle.bgcolor("red") turtle.fillcolor("yellow") def yyj_goto(x,y): turtle.up() turtle.goto(x,y) turtle.down() def yyj_draw(r): turtle.begin_fill() for i in range(5): turtle.forward(r) turtle.right(144) turtle.end_fill() yyj_goto(-600,220) yyj_draw(150) yyj_goto(-400,295) yyj_draw(50) yyj_goto(-350,212) yyj_draw(30) yyj_goto(-350,145) yyj_draw(30) yyj_goto(-400,90) yyj_draw(30)

技術分享  

畫鉆石花

import turtle turtle.color("blue") for i in range(35): turtle.right(12) for i in range(2): turtle.forward(150) turtle.right(30) turtle.forward(100) turtle.right(150)

技術分享  

學號輸入

def shibie(studentID): if len(studentID)<12: print("請輸入正確的學號!") elif studentID.isdigit() != True: print("請輸入正確的學號!") else: grade = studentID[0:4] profession = studentID[4:8] order = studentID[10:12] print("年級:{}級".format(grade)) if profession == "0611": print("專業:網絡工程") print("序號:{}".format(order)) studentID = input("請輸入學號:") shibie(studentID)

技術分享 

查看星期

def weekday(week): week=int(week) if 0<week<8: i=week-1 str=[‘星期一‘,‘星期二‘,‘星期三‘,‘星期四‘,‘星期五‘,‘星期六‘,‘星期日‘] print("數字{}是{}".format(week,str[i])) else: print("輸入有誤") week=input("輸入1~7內的數字:") weekday(week)

技術分享 

 

輸入身份證

import time #省份對應字典 provinces = { 44:‘廣東省‘, } def shibie(IDcard): province = IDcard[0:2] birthYear = IDcard[6:10] localYear = time.strftime(‘%Y‘) age = int(localYear) - int(birthYear) sex = IDcard[16:17] print("省份為:",provinces.get(int(province))) print("年齡為:{}".format(age)) if int(sex) % 2 == 0: print("性別:女") else: print("性別:男") IDcard = input("請輸入身份證:") shibie(IDcard)

技術分享  

課堂練習(條件、循環、函數定義、字符串操作)