Python基础综合练习

# coding=utf-8
import turtle


# 画五角星的方法
def drawPentagram(x):
    turtle.begin_fill()
    turtle.color('yellow')
    for i in range(5):
        turtle.forward(x)
        turtle.right(144)
    turtle.end_fill()


def gotoPoint(x,y,z):
    turtle.penup()
    turtle.setheading(0)
    turtle.goto(x,y)
    turtle.right(z)
    turtle.pendown()


#length = 540
length = int(input('请输入国旗长度:'))
width=length/3*2
bigDiameter=width*0.3
smallDiameter=width*0.1
turtle.hideturtle()

turtle.penup()
turtle.goto(-length/2,width/2)
turtle.pendown()
turtle.color('red')
turtle.begin_fill()
for i in range(2):
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(width)
    turtle.right(90)
turtle.end_fill()

# 大的五角星
gotoPoint(-width*0.65, width*0.3, 0)
drawPentagram(bigDiameter)

# 第一个小五角星
gotoPoint(-width*0.3, width*9/20, 12)
drawPentagram(smallDiameter)

# 第二个小五角星
gotoPoint(-width*0.18,width*0.32, -18)
drawPentagram(smallDiameter)

# 第三个小五角星
gotoPoint(-width*0.18,width*0.19, 0)
drawPentagram(smallDiameter)

# 第四个小五角星
gotoPoint(-width*0.3, width*0.1, 12)
drawPentagram(smallDiameter)

turtle.done()

  运行结果图:

 

字符串练习:

取得校园新闻的编号

url = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html'
print(url[-14:-5])

运行结果图

 

产生python文档的网址

def generatePythonUrl(className):
    addr1 = 'https://docs.python.org/3/library/'
    addr2 = '.html'
    return  addr1 + className + addr2
print(generatePythonUrl('turtle'))

运行结果图

 

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

for i in range(0,20):
    print('http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i))

运行结果图

 

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

用函数得到校园新闻编号

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

运行结果

 

 

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

lyric = '''
Waking up I see that everything is ok
The first time in my life
and now it's so great
Slowing down I look around
and I am so amazed
I think about the little things
that make life great
I wouldn't change a thing about it
This is the best feeling
This innocence is brilliant,
I hope that it will stay
This moment is perfect,
please don't go away,
I need you now
And I'll hold on to it,
don't you let it pass you by
I found a place so safe,
not a single tear
The first time in my life
and now it's so clear
Feel calm I belong,
I'm so happy here
It's so strong
and now I let myself be sincere
I wouldn't change a thing about it
This is the best feeling
This innocence is brilliant,
I hope that it will stay
This moment is perfect,
please don't go away,
I need you now
And I'll hold on to it,
don't you let it pass you by
It's the state of bliss you think
you're dreaming
It's the happiness inside
that you're feeling
It's so beautiful
it makes you wanna cry
It's the state of bliss you think
you're dreaming
It's the happiness inside
that you're feeling
It's so beautiful
it makes you wanna cry
It's so beautiful
it makes you want to cry
This innocence is brilliant,
it makes you want to cry
This innocence is brilliance,
please don't go away
Cause I need you now
And I'll hold on to it,
don't you let it pass you by
This innocence is brilliant,
I hope that it will stay
This moment is perfect,
please don't go away,
I need you now
And I'll hold on to it,
don't you let it pass you by
'''
result = lyric.replace(',', ' ').lower().lstrip().rstrip()
tempwords = result.split()
words = list(set(tempwords))
print(words)
print(result)
for i in range(0,len(words)):
    print('单词  '+ words[i] + ' 的出现次数为:'+str(result.count(str(words[i]))))

运行结果图

 

posted @ 2018-03-20 16:05  风丶轻轻  阅读(179)  评论(0编辑  收藏  举报