zoukankan      html  css  js  c++  java
  • 条件、循环、函数定义 练习

    1. 注意标准库的两种导入与使用方式,建议大家采用<库名>.<函数名>的方式。
    2. 对前面的代码进行优化,用for,while,if,def实现:
      画五角星
    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()

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

      画太阳花画
    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.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)

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

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

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

    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("
    input 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("
    hello"+ " " + student_number + "  thanks for your input"".")

    实例:已知‘星期一星期二星期三星期四星期五星期六星期日 ’,输入数字(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!')

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

    #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('性别:男')

  • 相关阅读:
    Ubuntu 16 安装redis客户端
    crontab 参数详解
    PHP模拟登录发送闪存
    Nginx配置端口访问的网站
    Linux 增加对外开放的端口
    Linux 实用指令之查看端口开启情况
    无敌的极路由
    不同的域名可以指向同一个项目
    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
    Redis 创建多个端口
  • 原文地址:https://www.cnblogs.com/huanglinsheng/p/7513503.html
Copyright © 2011-2022 走看看