结合二分法的算法思想来猜数字吧。通过二分法来猜数字可以帮助你每次筛选掉一半错误的数字,提高你的准确率。
import random
num = random.randint(1,100)
count = 7
print("猜数字挑战开始,你有七次机会")
while True:
guess = int(input("输入你猜的数字:"))
if guess == num:
print("恭喜你猜对了")
break
elif guess > num:
count -= 1
print("猜大了")
print("你还有", count, "次机会")
elif guess < num:
count -= 1
print("猜小了")
print("你还有", count, "次机会")
if count == 0:
print("挑战失败了....")
print("正确的数字是",num)
break