'''
-*- coding: utf-8 -*-
@Author : Qixi
@Time : 2022/1/27 15:52
@Software: PyCharm
@File : guessingGame.py
'''
from random import randint
from IPython.display import clear_output
guessRight = False
number = randint(0, 100)
guessTimes = 0
while not guessRight:
try:
ans = int(input("请输入你猜测的数字:"))
except ValueError:
print("请输入数字进行游戏")
else:
guessTimes += 1
clear_output()
if int(ans) == number:
print("恭喜您猜测正确,它就是{}!,截至游戏结束,你一共猜测{}次。".format(number,guessTimes))
break
elif int(ans) > number:
print("猜测数字过大")
elif int(ans) < number:
print("猜测数字过小")
运行结果:
|