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

    1. 用循环画五角星
      import turtle
      turtle.color ('red')
      turtle.fillcolor ('yellow')
      turtle.begin_fill ()
      for i in range (5):
          turtle.forward (100)
          turtle.right (144)
      turtle.end_fill ()

       

    2. 用循环画同心圆
      import turtle
      from turtle import *
      for i in range(10):
          turtle.up ()
          turtle.goto(0,-20*(i+1))
          turtle.down ()
          turtle.circle(20*(i+1))

       

    3. 用while循环画太阳花
      import turtle
      from turtle import *
      while True:
          forward(200)
          right(170)
          if(abs(pos()))<1:
              break

       

    4. 用函数定义画五个五角星
      import turtle
      turtle.color ('red')
      turtle.bgcolor ('red')
      turtle.color ('yellow')
      turtle.fillcolor ('yellow')
      def lcm_draw5(r):
          turtle.begin_fill ()
          for i in range(4):
              turtle.forward (r)
              turtle.right (144)
          turtle.end_fill()
      def lcm_location(x,y):
          turtle.up ()
          turtle.goto(x,y)
          turtle.down ()
      lcm_location(-250,75)
      lcm_draw5(100)
      
      
      lcm_location(-80,125)
      lcm_draw5(50)
      
      
      lcm_location(-60,100)
      lcm_draw5(50)
      
      lcm_location(-80,-20)
      lcm_draw5(50)
      
      lcm_location(-70,-60)
      lcm_draw5(50)

       

    5. 用函数定义画钻石花瓣的太阳花
      import turtle
      def lcm_draw4():
          turtle.forward(100)
          turtle.right(45)
          turtle.forward(100)
          turtle.right(135)
      
      for i in range(36):
          lcm_draw4()
          lcm_draw4()
          turtle.left(10)

       

  1. 字符串操作
    1. 输入学号,识别年级、专业、序号。
      num=input('输入您的学号:')
      print('年级:'+num[:4] ,'专业:'+num[4:8], '序号:'+num[8:])

       

    2. 输入1-7的数字,输出对应的“星期几”。
      s=('一二三四五六日')
      day=int(input('输入数字(1-7):'))
      print('星期'+s[day-1])

       

    3. 识别身份证号中的省市区、年龄、性别。
    4. 用字符串操作生成python文档各库的网址(起始网址在这里https://docs.python.org/3.6/library/index.html)
      a=('https://docs.python.org/3.6/library/')
      b=('.html')
      print(a[:]+'index'+b[:])

       

posted on 2017-09-14 18:58  ohh咔咔咔  阅读(121)  评论(0编辑  收藏  举报

导航