Commit ecb1edfa by BellCodeEditor

auto save

parent 1bad0edb
Showing with 22 additions and 21 deletions
# 悟空想海龟作图画出弹簧隧道,但是出现了bug,运行不了,请你帮助他,修改bug~ import random
import turtle alist = []
for i in range(1,101):
alist.append(i)
num = random.choice(alist)
pen=turtle.Pen() #自定义函数 实现二分查找
screen=turtle.Screen() def search_num(alist,num):
pen.speed() low = 0 #最低索引
pen.hideturtle() high = len(alist)-1 #最高索引
screen.bgcolor('black') while low<=high:
mid = (low+high)//2 #中间索引
i=0 guess = alist[mid] #根据中间索引获取到的值
while i<135: if guess==num:
pen.pencolor('pink') return mid;
pen.penup() elif guess<num:
pen.goto(0,0) low = mid+1
pen.forward(200) else:
pen.pendown() high = mid-1
pen.circle(100) return None
pen.left(2) result = search_num(alist,num)
i+=1 print('要查找的数为:',num)
print('索引是:',result)
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