课堂练习(条件、循环、函数定义、字符串操作)

画五角星

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)

  

时间: 2024-08-03 08:24:14

课堂练习(条件、循环、函数定义、字符串操作)的相关文章

阶乘循环--函数定义、函数调用

#! /bin/bash # using recursion function factorial() { if [ $1 -eq 1 ]; then echo 1 else local temp=$[ $1 -1 ] local result=`factorial $temp` echo $[ $result * $1] fi } read -p "Enter value:" value result=`factorial $value` echo "The factori

make--变量 条件判断 函数定义及调用

一.变量的高级主题 A.变量值的替换1.使用指定字符(串)替换变量中的后缀字符(串)2.语法格式:$(var:a=b) (将a替换成b)a.替换表达式中不能有任何的空格b.make中支持使用${}对变量进行取值示例 src:=a.cc b.cc c.cc obj:=$(src:cc=o) test: @echo "obj=>$(obj)" 输出结果由上图可以看出变量值的替换B.变量的模式替换1.使用%保留变量值中的指定字符,替换其它字符2.语法格式:$(var:a%b=x%y)a

函数,字符串操作

函数 定义:功能代码的集合 function 方法名(参数列表){ 方法体 } 方法名(); //调用 另一种定义方法: var fun = function(){ //方法体 } fun();//该方式没有"预加载",必须先声明.后调用. 方法返回值 return; 结束方法,返回数据 函数的参数: 定义的时候:形参 调用的时候:实参 形参>实参:Undefined! 实参>形参:不显示! So:形参数量==实参数量!  魔术参数: arguments:用在方法内,代表实

条件循环函数练习

1.五角星import turtle turtle.setup(600,400,0,0) turtle.color('yellow') turtle.bgcolor('red') turtle.fillcolor('yellow') turtle.up() turtle.goto(-250,75) turtle.down() turtle.begin_fill() for i in range(5): turtle.forward(100) turtle.right(144) turtle.en

条件、循环、函数定义、字符串操作练习

注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式. 对前面的代码进行优化,用for,while,if,def实现: 用循环画五角星 1 import turtle 2 3 turtle.fillcolor("red") 4 turtle.begin_fill() 5 for i in range(5): 6 turtle.forward(100) 7 turtle.right(144) 8 turtle.end_fill() 用循环画同心圆

【作业】条件、循环、函数定义、字符串操作练习

一.注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式. 二.对前面的代码进行优化,用for,while,if,def实现: 1.用循环画五角星 1 from turtle import* 2 color("red") 3 fillcolor("red") 4 begin_fill() 5 while True: 6 forward(200) 7 right(144) 8 if abs(pos())<1: 9 bre

条件、循环、函数定义、字符串操作

2-a.用循环画五角星 import turtle for i in range(5): turtle.forward(200) turtle.left(144) 2-b用循环画同心圆 import turtle turtle.color('green') for i in range(4): turtle.up() turtle.goto(0,-40*(i+1)) turtle.down() turtle.circle(40*(i+1)) 2-c.用while循环画太阳花 from turtl

条件、循环、函数定义、字符串操作练习9-14

用循环画五角星 >>> import turtle >>> for i in range(5): turtle.forward(100) turtle.right(144) 用循环画同心圆 >>> import turtle >>> for i in range(4): turtle.penup() turtle.goto(0,-40*i) turtle.pendown() turtle.circle(40*i)   用while循环

条件,循环,函数定义,字符串小练习

用for,while,if,def实现 1.用循环画五角星 1 from turtle import * 2 for i in range(5): 3 forward(110) 4 right(144) 2.用循环画同心圆 1 from turtle import * 2 for i in range(5): 3 pu() 4 goto(0,-(i+1)*10) 5 pd() 6 circle((i+1)*10) 3.用wlile循环画太阳花 1 color('red','yellow') 2

条件、循环、函数定义、字符串练习

1.用循环画五角星 import turtle turtle.setup(600,400,0,0) turtle.color("yellow") turtle.bgcolor('red') turtle.fillcolor("yellow") turtle.up() turtle.goto(-250,75) turtle.down() turtle.begin_fill() for i in range(5): turtle.forward(100) turtle.