Commit 0982fdc1 by BellCodeEditor

save project

parent 37ecb5f1
Showing with 35 additions and 20 deletions
import turtle
import random import random
alist = [] pen = turtle.Turtle()
for i in range(1, 101): pen.color('sienna')
alist.append(i) pen.speed(0)
num = random.choice(alist)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num): w = turtle.Screen()
less_num = num w.bgcolor('wheat')
high = len(alist)-1
low = alist[0] pen.left(90)
while low <= high: pen.up()
mid = (low+high) // 2 pen.backward(100)
guess = alist[mid] pen.down()
if guess == less_num:
return mid def tree(n):
elif guess < less_num: if n>=0:
low = mid+1 if n<=12:
color_list = ['snow','lightcoral']
color = random.choice(color_list)
pen.color(color)
pen.pensize(n/3)
else: else:
high = mid-1 pen.color('sienna')
binary_search(alist,num) pen.pensize(n/10)
print('给的数是',num) pen.forward(n)
\ No newline at end of file angle = random.random()
pen.right(30*angle)
length = 1.5*random.random()
tree(n-10*length)
pen.left(60*angle)
tree(n-10*length)
pen.right(30*angle)
pen.up()
pen.backward(n)
pen.down()
tree(60)
turtle.done()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment