- 注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
- 对前面的代码进行优化,用for,while,if,def实现:
- 用循环画五角星
import turtle turtle.color("yellow") turtle.fillcolor("yellow") turtle.speed(8) turtle.begin_fill() for i in range(5): turtle.forward(200) turtle.right(144) turtle.end_fill()
- 用循环画同心圆
from turtle import* for i in range(4): up() goto(0,-20*(i)) down() circle(20*i)
- 用while循环画太阳花
from turtle import* color('red','yellow') begin_fill() while True: forward(200) left(170) if (abs(pos()))<1: break end_fill() done()
- 用函数定义画五个五角星
import turtle turtle.setup(600,400,0,0) turtle.color("yellow") turtle.bgcolor("red") turtle.fillcolor("yellow") turtle.speed(5) def goto(x,y): turtle.up() turtle.goto(x,y) turtle.down() def bigstar(z): turtle.begin_fill() for i in range(5): turtle.forward(z) turtle.right(144) turtle.end_fill() goto(-250,125) bigstar(125) goto(-105,165) turtle.left(40) turtle.begin_fill() for i in range(5): turtle.forward(35) turtle.right(144) turtle.end_fill() goto(-30,135) turtle.right(10) turtle.begin_fill() for i in range(5): turtle.right(144) turtle.forward(35) turtle.end_fill() goto(-40,75) turtle.left(40) turtle.begin_fill() for i in range(5): turtle.right(144) turtle.forward(35) turtle.end_fill() goto(-75,20) turtle.right(20) turtle.begin_fill() for i in range(5): turtle.right(144) turtle.forward(35) turtle.end_fill()
- 用函数定义画钻石花瓣的太阳花
import turtle def draw_diamond(brad): brad.forward(100) brad.right(45) brad.forward(100) brad.right(135) def draw_art(): window=turtle.Screen() brad=turtle.Turtle() brad.color("red") 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()
- 用循环画五角星
- 字符串操作
- 输入学号,识别年级、专业、序号。
num=input("输入学号:") print("你的年级是{}级".format(num[0:4])) if(int(num[8:10]))==43 print("你的班级是网络工程一班") else print("你的班级未识别!") print("你在班级的序号是{}".format(num[-2:]))
- 输入1-7的数字,输出对应的“星期几”。
s='星期一星期二星期三星期四星期五星期六星期日' a=int(input("输入数字(1-7):")) if(7>a>1) print(s[-3+3*a:0+3*a]) else print("输入错误!")
- 识别身份证号中的省市区、年龄、性别。
num=input('请输入十八位身份证号码: ') if len(num)==18: print("你的身份证号码是 "+num) else: print("错误的身份证号码") num_add=ID[0:6] num_birth=ID[6:10] num_sex=ID[14:17] if int(num_add)==440101: print("省市区:广东省广州市市辖区") elif int(num_add)==440102: print("省市区:广东省广州市东山区") elif int(num_add)==440103: print("省市区:广东省广州市荔湾区") elif int(num_add)==440104: print("省市区:广东省广州市越秀区") elif int(num_add)==440105: print("省市区:广东省广州市海珠区") elif int(num_add)==440106: print("省市区:广东省广州市天河区") birth=2017-int(ID_birth[0:4]) print("年龄:{}".format(birth)) if int(ID_sex)%2==0: print('性别:女') else: print('性别:男')
- 输入学号,识别年级、专业、序号。