python基础题目100例之九十一
- 猜数游戏
import time
import random
num = random.randint(0,100)
scope = [0,99]
guess = int(input("输入你猜的数字 \n当前范围是%d-%d:"%(scope[0],scope[1])))
start_time = time.time()
while True:
if scope[0] > guess or guess > scope[1]:
guess = int(input("超出范围! \n当前范围是%d-%d:"%(scope[0],scope[1])))
elif guess > num:
scope[1] = guess
guess = int(input("猜大了,输入一个小的数字. \n当前范围是%d - %d :"%(scope[0],scope[1])))
elif guess < num:
scope[0] = guess
guess = int(input("输入一个大的数字. \n当前的范围是%d - %d :"%(scope[0],scope[1])))
else:
end_time = time.time()
print('猜对了,答案是 %d' % guess)
break
count_time = end_time - start_time
print('一共耗时 %d 秒' % count_time)