Python基础综合练习

# name=input("请输入您的名字:")
# print('您好,{}.'.format(name))


import turtle

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

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

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

mygoto(-270, 120)
drawF(120)

for i in range(4):
    x=1
    if i in [0, 3]:
        x=0
    mygoto(-120+x*30,150-i*40)
    turtle.left(15-i*15)
    drawF(30)

mygoto(0, 0)
turtle.hideturtle()
turtle.done()

字符串练习:

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

取得校园新闻的编号

 

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

 

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

产生python文档的网址

 

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

 

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

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

 

>>> candy=candy1+'turtle'+candy2
>>> print(candy)

 

练习字符串内建函数: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"))

 

用函数得到校园新闻编号

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)

 

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

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-21 20:13  241黄文麒  阅读(169)  评论(0编辑  收藏  举报