用循环画五角星
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,-50*i) turtle.pendown() turtle.circle(50*i)
用while循环画太阳花
import turtle turtle.color(‘red‘,‘yellow‘) turtle.speed(‘fastest‘) turtle.begin_fill() while True: turtle.forward(100) turtle.left(170) if abs(turtle.pos())<1: break turtle.end_fill() turtle.done()
用函数定义画五个五角星
import turtle def move(x,y): turtle.penup() turtle.goto(x,y) turtle.pendown() def drawstar(): for i in range(5): turtle.forward(100) turtle.right(144) for i in range(5): move(i*110,0) drawstar()
用函数定义画钻石花瓣的太阳花
import turtle def draw_diamond(brad): brad.forward(100) brad.right(45) brad.forward(100) brad.right(135) def draw_art(): window=turtle.Screen() window.bgcolor("white") brad=turtle.Turtle() brad.shape("turtle") brad.color("yellow") brad.speed("fastest") for i in range(0,36): draw_diamond(brad) draw_diamond(brad) brad.left(10) brad.right(90) brad.forward(155) brad.color(‘green‘) brad.forward(145) window.exitonclick() draw_art()
输入学号,识别年级、专业、序号
def decide(ID): if len(ID)<12: print("你输入的学号有误,请正确输入!") elif ID.isdigit() !=True: print("你输入的学号有误,请正确输入!") else: grade=ID[0:4] profession=ID[4:8] order=ID[10:12] print("{}级".format(grade),end="") if profession==‘0611‘: print("网络工程",end="") print("{}号同学".format(order)) ID=input("请输入学号:") decide(ID)
输入1-7的数字,输出对应的“星期几”。
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 = { 11:‘北京市‘, 12:‘天津市‘, 13:‘河北省‘, 14:‘山西省‘, 15:‘内蒙古自治区‘, 21:‘辽宁省‘, 22:‘吉林省‘, 23:‘黑龙江省‘, 31:‘上海市‘, 32:‘江苏省‘, 33:‘浙江省‘, 34:‘安徽省‘, 35:‘福建省‘, 36:‘江西省‘, 37:‘山东省‘, 41:‘河南省‘, 42:‘湖北省‘, 43:‘湖南省‘, 44:‘广东省‘, 45:‘广西壮族自治区‘, 46:‘海南省‘, 50:‘重庆市‘, 51:‘四川省‘, 52:‘贵州省‘, 53:‘云南省‘, 54:‘西藏自治区‘, 61:‘陕西省‘, 62:‘甘肃省‘, 63:‘青海省‘, 64:‘宁夏回族自治区‘, 65:‘新疆维吾尔自治区‘, 71:‘台湾省‘, 81:‘香港特别行政区‘, 91:‘澳门特别行政区‘ } def decide(cardID): province=cardID[0:2] birthdayYear=cardID[6:10] localYear=time.strftime(‘%Y‘) age=int(localYear)-int(birthdayYear) sex=cardID[16:17] print("省份为:", provinces.get(int(province))) print("年龄为:{}".format(age)) if int(sex)%2==0: print("性别:女") else: print("性别,男") cardID=input("请输入身份证号:") decide(cardID)
原文:http://www.cnblogs.com/amzinghui/p/7517900.html