条件循环函数定义 练习

Posted 杨小宏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了条件循环函数定义 练习相关的知识,希望对你有一定的参考价值。

1画五角星

import turtle


turtle.hideturtle()
turtle.speed(10)

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.end_fill()

2画同心圆

from turtle import*
for i in range(5):
    up()
    goto(0,-20*(i+1))
    down()
    circle(20*(i+1))

3画太阳花

from turtle import*

color(\'red\',\'yellow\')
begin_fill()
while True:
    forward(200)
    left(170)
    if(abs(pos()))<1:
        break
end_fill()
done()

4五个五角星

import turtle


turtle.hideturtle()
turtle.speed(10)

turtle.setup(600,400,0,0)
turtle.color("yellow")
turtle.bgcolor("red")
turtle.fillcolor("yellow")

def my_goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down()

def myforward(r):
    
    turtle.begin_fill()
    for i in range(5):
        turtle.forward(r)
        turtle.right(144)
    turtle.end_fill()

my_goto(-240,100)
myforward(100)

my_goto(-100,135)
myforward(30)

my_goto(-70,95)
myforward(30)

my_goto(-70,5
myforward(30)

my_goto(-100,15)
myforward(30)

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

import turtle

def draw_diamond(turt):

    for i in range(1,3):

        turt.forward(100)

        turt.right(45)

        turt.forward(100)

        turt.right(135)

def draw_art():

    window=turtle.Screen()

    window.bgcolor("green")

    brad=turtle.Turtle()

    brad.shape("turtle")

    brad.color("red")

    brad.speed(\'fast\')

    for i in range(1,37):

        draw_diamond(brad)

        brad.right(10)

        brad.right(90)

        brad.forward(300)

    window.exitonclick()

draw_art()

6实例:输入学号,识别年级、专业、序号

def get_student_number(class_id,system_id,major_id,student_id):
    full_number = class_id + system_id + major_id + student_id
    return full_number.title()

while True:
    print("\\ninput your full_number:")
    print("(enter \'q\' at any time to quit)")

    class_id = input("class_id:")
    if class_id == \'q\':
        break

    system_id = input("system_id:")
    if system_id == \'q\':
        break

    major_id = input("major_id:")
    if major_id == \'q\':
        break

    student_id = input("student_id:")
    if student_id == \'q\':
        break

    student_number = get_student_number(class_id,system_id,major_id,student_id)
    print("\\nhello"+ " " + student_number + "  thanks for your input"".")

7实例:已知‘星期一星期二星期三星期四星期五星期六星期日 ’,输入数字(1-7),输出相应的‘星期几’

#coding=gbk

x = input("Please enter an integer:")
x = int(x)
if x ==1 :
    print (\'星期一\')
elif x == 2:
    print (\'星期二\')
elif x == 3:
    print (\'星期三\')
elif x == 4:
    print (\'星期四\')
elif x == 5:
    print (\'星期五\')
elif x == 6:
    print (\'星期六\')
elif x == 7:
    print (\'星期七\')
else:
    print (\'the number is wrong!\')

8实例:输入身份证号,识别地区、年龄、性别

#coding=gbk
ID=input(\'请输入十八位身份证号码: \')
if len(ID)==18:
  print("你的身份证号码是 "+ID)
else:
  print("错误的身份证号码")
  
ID_add=ID[0:6]
ID_birth=ID[6:14]
ID_sex=ID[14:17]
ID_check=ID[17]
  
#ID_add是身份证中的区域代码,如果有一个行政区划代码字典,就可以用获取大致地址#
  
year=ID_birth[0:4]
moon=ID_birth[4:6]
day=ID_birth[6:8]
print("生日: "+year+\'\'+moon+\'\'+day+\'\')
  
if int(ID_sex)%2==0:
  print(\'性别:女\')
else:
  print(\'性别:男\')

 

以上是关于条件循环函数定义 练习的主要内容,如果未能解决你的问题,请参考以下文章

条件循环函数定义 练习

条件循环函数定义 练习

条件循环函数定义 练习

条件循环函数定义 练习

条件循环函数定义 练习

条件循环函数定义 练习