python格式化输出变量

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def binaray_search(list, item):
'''
   二分法搜索
   2018.3.12
   格式化输出
   输出搜索对象和搜索次数
   
'''
	low = 0
	high = len(list) - 1
	i = 0
	while(low <= high):	
		mid = (low + high)/2
		guess = mid
		if guess == item:
			i= i + 1
			print('got it!')
			return guess,i
			
		elif guess < item:
			low  = mid + 1
			i= i + 1
		else:
			high = mid - 1
			i= i + 1
	
	
if __name__ == '__main__':
	my_list = [1,2,3,4,5,6,7,8,9,10, 11,12]
	my_item = 11	
	a = binaray_search(my_list, my_item)[0]
	i= binaray_search(my_list, my_item)[1]
	print('to_fine_num is : %d, take %d times to find' %(a, i))
	


你可能感兴趣的:(Python学习)