2-a 用循环画五角星
import turtle for i in range(5): turtle.forward(200) turtle.left(144)
2-b 用循环画同心圆
import turtle turtle.color(‘red‘) for i in range(4): turtle.up() turtle.goto(0,-40*(i+1)) turtle.down() turtle.circle(40*(i+1))
2-c 用while循环画太阳花
from turtle import * color(‘red‘,‘yellow‘) begin_fill() while True: forward(200) left(170) if abs(pos())<1: break end_fill() done()
2-d 用函数定义画五个五角星
import turtle turtle.bgcolor(‘red‘) turtle.color(‘yellow‘) turtle.fillcolor(‘yellow‘) def guo_goto(x,y): turtle.up() turtle.goto(x,y) turtle.down() def guo_draw(x): turtle.begin_fill() for i in range(5): turtle.forward(x) turtle.right(144) turtle.end_fill() guo_goto(-300,200) guo_draw(170) guo_goto(30,280) guo_draw(60) guo_goto(120,200) guo_draw(60) guo_goto(120,126) guo_draw(60) guo_goto(30,60) guo_draw(60)
2-e 用函数定义画钻石花瓣的太阳花
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("purple") brad=turtle.Turtle() brad.shape("turtle") brad.color("orange") brad.speed("fastest") for i in range(1,37): draw_diamond(brad) draw_diamond(brad) brad.left(10) brad.right(90) brad.pensize(5) brad.color(‘green‘) brad.forward(300) window.exitonclick() draw_art()
3-a 输入学号,识别年级、专业、序号。
s=input("Please input your Student Number:") print("年级是",s[:4]) print("专业是",s[4:6]) print("序号是",s[-3:])
3-b 输入1-7的数字,输出对应的“星期几”。
a=‘星期一星期二星期三星期四星期五星期六星期日‘ d=int(input(‘1-7:‘)) print(a[3*(d-1):3*d])
3-c 识别身份证号中的省市区、年龄、性别。
ID=input(‘请输入十八位身份证号码: ‘) if len(ID)==18: print("你的身份证号码是 "+ID) else: 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(‘性别:男‘)
3-d 用字符串操作生成python文档各库的网址(起始网址在这里https://docs.python.org/3.6/library/index.html)
3-e 练习字符串的+,*,in,len(),eval()
时间: 2024-10-05 03:55:21