用循环画五角星
import turtleturtle.begin_fill()for i in range(5): turtle.forward(100) turtle.right(144)turtle.end_fill()
用循环语句画同心圆
from turtle import*for i in range(5): up() goto(0,-20*i) down() circle(20*i)
用while循环画太阳花
from turtle import *color('orange','yellow')begin_fill()while True: forward(200) left(170) if(abs(pos()))<1: breakend_fill()done()
函数定义画五个五角星
import turtleturtle.setup(600,400,0,0)turtle.color("yellow")turtle.bgcolor('red')turtle.fillcolor("yellow") def xy(x,y): turtle.up() turtle.goto(x,y) turtle.down() def draw(r): turtle.begin_fill() for i in range(5): turtle.forward(r) turtle.right(144) turtle.end_fill() xy(-273,110)draw(100) xy(-121,162)turtle.left(45)draw(50) xy(-79,110)draw(50) xy(-79,51)turtle.right(45)draw(50) xy(-121,22)turtle.right(45)draw(50)
用函数定义画钻石花瓣的太阳花
import turtle def draw_diamond(brad): brad.forward(120) brad.right(45) brad.forward(120) brad.right(135) def draw_art(): window=turtle.Screen() window.bgcolor("pink") brad=turtle.Turtle() brad.shape("turtle") brad.color("red") for i in range(1,13): draw_diamond(brad) draw_diamond(brad) brad.left(30) window.exitonclick() draw_art()
字符串操作
.输入学号,识别年级、专业、序号
professions = { 1144:'网络工程', 1143:'网络工程', 1142:'软件工程', 1141:'数字媒体', } def distinguish(studentID): if len(studentID)<12: print("请输入正确的学号!") elif studentID.isdigit() != True: print("请输入正确的学号!") else: grade = studentID[0:4] profession = studentID[6:10] num = studentID[10:12] print("年级: {}级".format(grade)) print("专业:",professions.get(int(profession))) print("序号: {}号".format(num)) studentID = input("请输入学号:")distinguish(studentID)
输入1-7的数字,输出对应的“星期几”
s="星期一星期二星期三星期四星期五星期六星期天"i=int(input("请输入(1-7):"))if(0<8): print(s[-3+3*i:0+3*i])else: print("输入有误!")
识别身份证号中的省市区、年龄、性别
ID=input('请输入十八位身份证号码: ')if len(ID)!=18: print("错误的身份证号码!!") ID_add=ID[0:6]ID_birth=ID[6:10]ID_sex=ID[14:17] if int(ID_add)==440101: print("省市区:广东省广州市市辖区")elif int(ID_add)==440102: print("省市区:广东省广州市东山区")elif int(ID_add)==440103: print("省市区:广东省广州市荔湾区")elif int(ID_add)==440104: print("省市区:广东省广州市越秀区")elif int(ID_add)==440105: print("省市区:广东省广州市海珠区") birth=2017-int(ID_birth[0:4])print("年龄:{}".format(birth)) if int(ID_sex)%2==0: print('性别:女')else: print('性别:男')
用字符串操作生成python文档各库的网址(起始网址在这里)
练习字符串的+,*,in,len(),eval()