猜0-100(不包括100)中奖数字,每次都提示新的猜测范围:
如数字88,猜测12,提示太小了,请猜一个13-100的数字
# 生成中奖数字
winning_number = random.randint(0, 99)
print(f"中奖数字,你来猜猜是多少?")
#winning_number=88
guess_count = 0
guess1 = -1
guess2 = 100
guess = int(input(f"请输入你的猜测数字(0-100)不包含100: "))
'''while guess < 0 or guess >= 100:
guess=int(input(f"输入的数字不在0到100的范围内,请重新输入:"))
continue
for i in range(100):
if guess < 0 or guess >= 100:
guess = int(input(f"输入的数字不在0到100的范围内,请重新输入:"))
continue
else:
break'''
# 循环直到猜中为止
while True:
guess_count += 1
if guess < winning_number:
if guess <= guess1 or guess>=guess2:
if guess < 0 or guess >= 100:
guess = int(input(f"输入的数字不在0到100的范围内,请重新输入:"))
else:
guess = int(input(f"为了更快速的猜中,请重新输入一个{guess1}-{guess2}的猜测数字哦: "))
else:
guess1 = guess
guess=int(input(f"太小了,请猜一个{guess1}-{guess2}之间的数字:"))
continue
elif guess > winning_number:
if guess <= guess1 or guess>=guess2:
if guess < 0 or guess >= 100:
guess = int(input(f"输入的数字不在0到100的范围内,请重新输入:"))
else:
guess = int(input(f"为了更快速的猜中,请重新输入一个{guess1}-{guess2}的猜测数字哦: "))
else:
guess2 = guess
guess=int(input(f"太大了,请猜一个{guess1}-{guess2}之间的数字"))
continue
else:
print(f"恭喜你,猜对了!中奖数字是{winning_number}!")
print(f"你一共猜了{guess_count}次才猜中。")
break