第一步 新建py文件

新建txt文件,将名字改为 guess.py,复制以下内容,粘贴到guess.py文件中保存

第二步 运行python

在文件夹地址栏输入 cmd 回车,在cmd中输入 python guess.py 运行看结果

python 猜数字游戏

import random

def guess_number():
number_to_guess = random.randint(1, 100)
attempts = 0
max_attempts = 10

print("欢迎来到猜数字游戏!")
print("我已经选择了一个 1 到 100 之间的数字。你有 10 次机会来猜它。")

while attempts < max_attempts:
guess = int(input("请输入你的猜测:"))
attempts += 1

if guess < number_to_guess:
print("太小了!")
elif guess > number_to_guess:
print("太大了!")
else:
print(f"恭喜你!你猜对了,数字是 {number_to_guess}。")
break
else:
print(f"很遗憾,你没有猜对。正确的数字是 {number_to_guess}。")

guess_number()

下载代码

https://www.18k.icu/code/python/guess.py