python基础综合练习

1.画一面五星红旗:

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

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

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

mygoto(-200,100)
drawwujiaoxing(120)


for i in range(4):
    x=1;
    if i in [0,3]:
        x=0;
    mygoto(-60+x*50,150-i*50)
    turtle.left(0-i*10)
    drawwujiaoxing(30)

turtle.hideturtle()
turtle.done()

结果截图: 

 

 

2.取得校园新闻的编号

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

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

结果截图: 

3.产生python文档的网址

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

addr1 = "https://docs.python.org/3.6/library/"
addr2 = ".html"
addr =addr1+"turtle"+addr2 

结果截图:

4.产生校园新闻的一系列新闻页网址

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

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

结果截图:

 

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

str ="      I'm very handsome!   "
print(str.lstrip())
print(str.count("e"))
print(str.split())
print(str.split().replace("handsome","perfect"))

结果截图: 

6.用函数得到校园新闻编号:

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

结果截图: 

  

7.用函数统计一歌词(文章、小说)中单词出现的次数,替换标点符号为空格,用空格进行分词。

str = '''
I like doing sports and reading in my free time. 
My favorite sport is basketball. 
I often play basketball with my classmates after school and play with my friends on weekends. 
I like playing basketball because it can make me strong and health. When I play basketball, 
I forget all my worries and I can make lots of friends who have the same interest. 
In the evening when I finish doing my homework, 
I like reading because I can get lots of knowledge from books and the books can tell me more about China and the world. 
Books are our best friends and they can open our eyes,too. I like all kinds of books, story book, novels and so on.
'''

print(str.count("my"))
print(str.count("I"))

print(str.replace(","," "))

结果截图:

  

  

 

  

 

posted @ 2018-03-20 21:38  232许健杰  阅读(138)  评论(0编辑  收藏  举报