【Python小练习】猜数字小游戏

#!/usr/bin/python3
'''
-*- coding: utf-8 -*-
@Author  : Qixi
@Time    : 2022/1/27 15:52
@Software: PyCharm
@File    : guessingGame.py
'''
# guessing_game

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("猜测数字过小")

运行结果:
【Python小练习】猜数字小游戏_第1张图片

你可能感兴趣的:(代码开发,python,小程序,pycharm)