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

注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。

对前面的代码进行优化,用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()

用循环画同心圆

1 import turtle
2 for i in range(3):
3     turtle.up()
4     turtle.goto(0,-50*(i+1))
5     turtle.down()
6     turtle.circle(50*(i+1))

用while循环画太阳花

1 import turtle
2 turtle.color(‘red‘,‘yellow‘)
3 turtle.begin_fill()
4 while True:
5     turtle.forward(200)
6     turtle.left(170)
7     if abs(turtle.pos()) < 1:
8         break
9 turtle.end_fill()

用函数定义画五个五角星

 1 import turtle
 2 turtle.setup(600,400,0,0)
 3 turtle.color("yellow")
 4 turtle.bgcolor(‘red‘)
 5 turtle.fillcolor("yellow")
 6
 7 def Mygoto(x,y):
 8     turtle.up()
 9     turtle.goto(x,y)
10     turtle.down()
11
12 def Mydraw(r):
13     turtle.begin_fill()
14     for i in range(5):
15         turtle.forward(r)
16         turtle.right(144)
17     turtle.end_fill()
18
19 Mygoto(-273,110)
20 Mydraw(100)
21
22 Mygoto(-121,162)
23 turtle.left(45)
24 Mydraw(50)
25
26 Mygoto(-79,110)
27 Mydraw(50)
28
29 Mygoto(-79,51)
30 turtle.right(45)
31 Mydraw(50)
32
33 Mygoto(-121,22)
34 turtle.right(45)
35 Mydraw(50)

用函数定义画钻石花瓣的太阳花

 1 import turtle
 2 def Yyxdraw_diamond(brad):
 3     brad.forward(120)
 4     brad.right(45)
 5     brad.forward(120)
 6     brad.right(135)
 7 def Yyxdraw_art():
 8     window=turtle.Screen()
 9     window.bgcolor("red")
10     brad=turtle.Turtle()
11     brad.shape("turtle")
12     brad.color("yellow")
13     for i in range(36):
14         Yyxdraw_diamond(brad)
15         Yyxdraw_diamond(brad)
16         brad.left(10)
17     window.exitonclick()
18
19 Yyxdraw_art()

字符串操作

输入学号,识别年级、专业、序号。

 1 majors = {
 2   11:‘网络工程‘,
 3   12:‘软件工程‘,
 4   13:‘数字媒体‘,
 5   }
 6
 7 def recognition(studentID):
 8      if len(studentID)<12:
 9          print("请输入正确的学号!")
10      elif studentID.isdigit() != True:
11          print("请输入正确的学号!")
12      else:
13          grade = studentID[0:4]
14          major = studentID[6:8]
15          num = studentID[10:12]
16          print("年级:{}级".format(grade))
17          print("专业为:",majors.get(int(major)))
18          print("序号:{}".format(num))
19
20 studentID = input("请输入学号:")
21 recognition(studentID)

输入1-7的数字,输出对应的“星期几”。

1 s="星期一星期二星期三星期四星期五星期六星期天"
2 i=int(input("请输入(1-7):"))
3 if(0<i<8):
4     print(s[-3+3*i:0+3*i])
5 else:
6     print("输入有误!")

识别身份证号中的省市区、年龄、性别。

 1 ID=input(‘请输入十八位身份证号码: ‘)
 2 if len(ID)!=18:
 3   print("错误的身份证号码!!")
 4
 5 ID_add=ID[0:6]
 6 ID_birth=ID[6:10]
 7 ID_sex=ID[14:17]
 8
 9 if int(ID_add)==440101:
10   print("省市区:广东省广州市市辖区")
11 elif int(ID_add)==440102:
12     print("省市区:广东省广州市东山区")
13 elif int(ID_add)==440103:
14     print("省市区:广东省广州市荔湾区")
15 elif int(ID_add)==440104:
16     print("省市区:广东省广州市越秀区")
17 elif int(ID_add)==440105:
18     print("省市区:广东省广州市海珠区")
19
20
21 birth=2017-int(ID_birth[0:4])
22 print("年龄:{}".format(birth))
23
24 if int(ID_sex)%2==0:
25   print(‘性别:女‘)
26 else:
27   print(‘性别:男‘)

用字符串操作生成python文档各库的网址(起始网址在这里https://docs.python.org/3.6/library/index.html)

1 yyx1 = "https://docs.python.org/3.6/library/"
2 yyx2 = ".html"
3 yyx3 = input("请输入库名:")
4 print("网址为:%s%s%s"%(yyx1,yyx3,yyx2))

练习字符串的+,*,in,len(),eval()

1 print("No!"*3)
2 s="this is "
3 t="younger!"
4 print(s+t)
5 p="hello world!!!"
6 for i in range(len(s)):
7     print(i,s[i])

时间: 2024-08-01 22:46:50

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

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

#! /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.用循环画五角星 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.