码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-14 20:21:38      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:网址   break   color   rom   def   index   ref   class   div   

    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[:])

       

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

标签:网址   break   color   rom   def   index   ref   class   div   

原文地址:http://www.cnblogs.com/lcm1995/p/7522222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!