標籤:pre 產生 str highlight ref can imp row lov
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=125 turtle.right(5)26 if i in [0,3]:27 x=028 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"+str2print(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 usYou have come to show you go on.Near, far, wherever you areI believe that the heart does go onOnce more you open the doorAnd you‘re here in my heartAnd my heart will go on and onLove can touch us one timeAnd last for a lifetimeAnd never go till we‘re goneLove was when I loved youOne true time I hold toIn my life we‘ll always go onNear, far, wherever you areI believe that the heart does go onOnce more you open the doorAnd you‘re here in my heartAnd my heart will go on and onYou‘re here, there‘s nothing I fear,And I know that my heart will go onWe‘ll stay forever this wayYou are safe in my heartAnd 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(","," "))
Python基礎綜合練習