1、首先,打開python軟件,進入首頁,然后在首頁中,點擊導入科赫雪花。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、馬龍網(wǎng)絡(luò)推廣、小程序制作、馬龍網(wǎng)絡(luò)營銷、馬龍企業(yè)策劃、馬龍品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供馬龍建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:chinadenli.net
2、其次,導入之后,點擊模塊選項,然后使用內(nèi)置模塊turtle。
3、最后,使用后即可改為90度。
import random
import turtle
def random_color():
rgbl=[255,0,0]
random.shuffle(rgbl)
return tuple(rgbl)
def koch(size,n):
if n==0:
? turtle.fd(size)
else:
? for angle in [0,60,-120,60]:
? ? ? cc = random_color()
? ? ? turtle.pencolor(cc[0], cc[1], cc[2])
? ? ? turtle.left(angle)
? ? ? koch(size/3,n-1)
def main():
turtle.colormode(255)
turtle.setup(600,600)
turtle.penup()
turtle.goto(-200,100)
turtle.pendown()
turtle.pensize(2)
level=4? ?#4階科赫雪花,階數(shù)
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.hideturtle()
turtle.done()
main()
效果如圖:
import turtle
def kehe(long,n):
if n == 0:
turtle.fd(long)
else:
for angle in [0,90,-90,-90,90]:
turtle.left(angle)
kehe(long/2,n-1)
def main():
turtle.setup(600,600)
turtle.penup()
turtle.speed(0)
turtle.goto(-200,100)
turtle.pendown()
turtle.pensize(2)
level = 2
for i in range(4):
kehe(50,level)
turtle.right(90)
turtle.hideturtle()
main()
for 變量 in range(次數(shù)):被執(zhí)行的語句? ? ? ? ? ? ? ? ? ? ? ?變量:表示每次循環(huán)的次數(shù),0-1之間
range(n)n表示產(chǎn)生0到n-1的整數(shù)序列共N個? ? ? ? ? ? ? ?range(m,n)? 產(chǎn)生m到n-1的整數(shù)序列,共n-m個
循環(huán)for語句? :for 循環(huán)變量 in遍歷結(jié)構(gòu):語句體1? else:語句體2?
無限循環(huán): while條件: 語句塊
while 條件:語句體1 else: 語句體2
循環(huán)保留字:break? ? ?continue
方法1:from random import random
from time import perf_counter
DARTS=1000
hits=0.0
start =perf_counter()
for i in range(1,DARTS+1):
x,y=random(),random()
dist=pow(x**2+y**2,0.5)
if dist=1.0:
? ? hits =hits+1
pi=4*(hits/DARTS)
print("圓周率是:{}".format(pi))
print("運行時間是{:.5f}s".format(perf_counter()-start))
方法2:
pi=0
n=100
for k in range(n):
pi += 1/pow(16,k)*(\
? ? 4/(8*k+1)-2/(8*k+4) - \
? ? 1/(8*k+5) - 1/(8*k+6))
print("圓周率值是:{}".format(pi))
def 函數(shù)名 (0個或者多個):函數(shù)體? renturn 返回值
def 函數(shù)名 (非可選參數(shù),可選參數(shù)):函數(shù)體? renturn 返回值
參數(shù)傳遞的兩種方式:位置傳遞,名稱傳遞
科赫雪花:
import turtle
def koch(size,n):
if n==0:
? ? turtle.fd(size)
else:
? ? for angle in [0,60,-120,60]:
? ? ? ? turtle.left(angle)
? ? ? ? koch(size/3,n-1)
def main():
turtle.setup(400,200)
turtle.penup()
turtle.pendown()
turtle.pensize(2)
l=3
koch(600,l)
turtle.right(120)
turtle.pencolor('blue')
koch(600,l)
turtle.right(120)
turtle.pencolor('red')
koch(600,l)
turtle.speed(3000)
turtle.hideturtle()
main()
階乘:
def fact(n):
s=1
for i in range(1,n+1):
? ? s*=i
return s
c=eval(input("從鍵盤輸入一個數(shù)字"))
print("階乘結(jié)果",fact(c))
這是一人遞歸調(diào)用,koch中,又調(diào)用了自已,結(jié)束條件是,n==0, 每次遞歸取1/3的size, 然后n-1 ,直到n==0結(jié)束。如果你不了解歸函數(shù)可以baidu一下。
新聞名稱:python科赫函數(shù) python科赫曲線的代碼講解
轉(zhuǎn)載注明:http://chinadenli.net/article48/doojeep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計、做網(wǎng)站、ChatGPT、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)