import random
n = random.randint(1,101) #指定范围为1-100
cut = 0
while True :
ans = int(input("请输入一个数字:"))
cut+ = 1
if ans == n :
print("猜对了,你一共猜了%d次"%cut)
elif ans<n :
print("小了")
else :
print("大了")
在我们和电脑玩猜数字的小游戏时,我们可以用数学上的“二分法”,最多猜七次就会猜出答案