Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
Guess Num.py 914 Bytes
Copy Edit Raw Blame History
dorabmon authored 2023-10-10 10:28 . 2rd test
# -*- coding: utf-8 -*-
"""
@Time : 2023/10/10 9:58
@Auth : dorabmon
@File :Guess Num.py
@IDE :PyCharm
"""
import random
# 生成一个随机数
num = random.randint(1, 100)
# 初始化猜测次数
guesses = 0
print("欢迎来到猜数游戏!我已经生成了一个1到100之间的数字。")
while True:
# 获取玩家的猜测
guess = input("请猜一个数字: ")
# 尝试将玩家的输入转换为整数
try:
guess = int(guess)
except ValueError:
print("请输入一个有效的数字。")
continue
# 增加猜测次数
guesses += 1
# 检查玩家的猜测
if guess < num:
print("太小了,再试试大一点的数字。")
elif guess > num:
print("太大了,再试试小一点的数字。")
else:
print(f"恭喜你猜对了!秘密数字是 {num},你猜了 {guesses} 次。")
break
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化