• Python基础综合练习


    1.综合练习:画一面五星红旗,将代码与运行截图发布博客交作业。

     代码如下:

     1 import turtle
     2 
     3 def mygoto(x, y):
     4     turtle.up()
     5     turtle.goto(x, y)
     6     turtle.down()
     7 
     8 def drow(x):
     9     turtle.begin_fill()
    10     for i in range(5):
    11         turtle.forward(x)
    12         turtle.right(144)
    13     turtle.end_fill()
    14 
    15 turtle.setup(600,400,0,0)
    16 turtle.color("yellow")
    17 turtle.bgcolor("red")
    18 turtle.fillcolor("yellow")
    19 
    20 mygoto(-250,95)
    21 drow(100)
    22 
    23 for i in range(4):
    24     x=1
    25     turtle.right(5)
    26     if i in [0,3]:
    27         x=0
    28     mygoto(-135+x*30,155-i*45)
    29     turtle.left(20-i*15)
    30     drow(30)
    31 
    32 turtle.hideturtle()
    33 turtle.done()

    截图:

    2.字符串练习:

    http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html

    取得校园新闻的编号,代码及截图如下:

    2.1:

    str = "http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
    num = str.rstrip(".html").split("_")[1]
    print(num)
    

      

     2.2

    https://docs.python.org/3/library/turtle.html

    产生python文档的网址

    str1 = "https://docs.python.org/3.6/library/"
    str2 = ".html"
    str =str1+"turtle"+str2
    print(str)
    

      

    2.3 

    http://news.gzcc.cn/html/xiaoyuanxinwen/4.html

    产生校园新闻的一系列新闻页网址,代码及截图如下:

    for i in range(2,10):
      str = "http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html".format(i)
      print(str)
    

      

    3.练习字符串内建函数:strip,lstrip,rstrip,split,count,replace

    3.1

    用函数得到校园新闻编号,代码及截图如下:

    str1 = "http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html"
    for i in range(2, 10):
        str2 = str1.format(i)
        str3 = str2.rstrip(".html").split("/")[-1]
        print(str3)
    

      

    3.2

    用函数统计一歌词(文章、小说)中单词出现的次数,替换标点符号为空格,用空格进行分词。代码及截图如下:

    str = '''
    Every night in my dreams,
    I see you. I feel you.
    That is how I know you go on.
    Far across the distance,
    And spaces between us
    You have come to show you go on.
    Near, far, wherever you are
    I believe that the heart does go on
    Once more you open the door
    And you're here in my heart
    And my heart will go on and on
    Love can touch us one time
    And last for a lifetime
    And never go till we're gone
    Love was when I loved you
    One true time I hold to
    In my life we'll always go on
    Near, far, wherever you are
    I believe that the heart does go on
    Once more you open the door
    And you're here in my heart
    And my heart will go on and on
    You're here, there's nothing I fear,
    And I know that my heart will go on
    We'll stay forever this way
    You are safe in my heart
    And my heart will go on and o'''
    print(str.count("my"))
    print(str.count("heart"))
    print(str.count("go"))
    print(str.count("on"))
    print(str.replace(","," "))
    

      

  • 相关阅读:
    JavaScript打造很酷的图片放大效果实例代码
    【荐】CSS实现的鼠标点击小图无刷新放大图片代码
    JavaScript+CSS实现的文字幻灯切换代码
    【荐】很棒的图片友情链接带控制按钮的横向滚动代码
    jquery制作一个漂亮带渐隐效果的跑动区域
    JS打造的一款响应鼠标变化很炫的图片特效代码
    JS+CSS控制鼠标移上图片滑出文字提示代码
    Jquery+CSS实现的大气漂亮图片切换效果代码
    【荐】JS+CSS防FLASH效果带倒影的图片切换效果代码
    JavaScript限制对图片右键代码
  • 原文地址:https://www.cnblogs.com/REGzjm85/p/8613486.html
Copyright © 2020-2023  润新知