猜数游戏的python程序

这是一个简单的猜数游戏的Python程序。在这个游戏中,计算机会随机选择一个1到100之间的数,然后让用户尝试猜测这个数。

复制代码
import random

def game():
    number_to_guess = random.randint(1, 100)
    guess = None
    attempts = 0

    while guess != number_to_guess:
        guess = int(input("请猜一个1到100之间的数: "))
        attempts += 1
        if guess < number_to_guess:
            print("太低了!再试一次。")
        elif guess > number_to_guess:
            print("太高了!再试一次。")
    
    print(f"恭喜你,你猜对了!这个数就是 {number_to_guess}。")
    print(f"你一共尝试了 {attempts} 次。")

if __name__ == "__main__":
    game()

这个程序会持续要求用户输入猜测的数字,直到猜对为止。如果用户的猜测数字低于目标数字,它会输出 "太低了!再试一次。",如果用户的猜测数字高于目标数字,它会输出 "太高了!再试一次。" 如果用户猜对了数字,程序会告诉用户他们猜对了,并显示他们尝试的次数。

我们可以增加一些新的功能,比如:记录用户的名字,记录他们猜的次数,让用户可以选择是否再玩一次,以及显示用户的历史记录。

复制代码
import random

def game():
    number_to_guess = random.randint(1, 100)
    guess = None
    attempts = 0
    player_name = input("请输入你的名字: ")
    game_history = []

    while guess != number_to_guess:
        guess = int(input(f"{player_name}, 请猜一个1到100之间的数: "))
        attempts += 1
        game_history.append((guess, attempts))
        if guess < number_to_guess:
            print("太低了!再试一次。")
        elif guess > number_to_guess:
            print("太高了!再试一次。")
    
    print(f"恭喜你,{player_name},你猜对了!这个数就是 {number_to_guess}。")
    print(f"你一共尝试了 {attempts} 次。")
    game_history.append((number_to_guess, attempts))
    print(f"你的游戏历史记录: {game_history}")

    play_again = input("你想再玩一次吗? (是/否): ")
    if play_again.lower() == "是":
        game()
    else:
        print("好的,谢谢参与!")

if __name__ == "__main__":
    game()

在这个新版本的游戏中,我们添加了记录玩家名字、猜的次数、游戏历史记录的功能,并且在玩家猜对数字后询问他们是否想再玩一次。如果他们想再玩一次,我们会重新开始游戏;否则,我们会结束程序。

我们可以增加游戏的难度,比如:让用户可以选择难度,在高级模式下,数字的范围会更大,而且会加入随机数干扰用户的猜测。我们还可以让用户输入他们的记录密码,以便在下次运行游戏时恢复他们的记录。

复制代码
import random

def game():
    number_to_guess = random.randint(1, 100)
    guess = None
    attempts = 0
    player_name = input("请输入你的名字: ")
    game_history = []
    difficulty = input("请选择难度 (简单/高级): ")

    if difficulty.lower() == "高级":
        number_to_guess = random.randint(1, 200)
        random_number = random.randint(1, 20)
        while guess != number_to_guess:
            if random_number == 1:
                guess = int(input(f"{player_name}, 请猜一个1到200之间的数: "))
            else:
                guess = int(input(f"{player_name}, 请猜一个1到100之间的数: "))
            attempts += 1
            game_history.append((guess, attempts))
            if guess < number_to_guess:
                print("太低了!再试一次。")
            elif guess > number_to_guess:
                print("太高了!再试一次。")
        print(f"恭喜你,{player_name},你猜对了!这个数就是 {number_to_guess}。")
        print(f"你一共尝试了 {attempts} 次。")
        game_history.append((number_to_guess, attempts))
        print(f"你的游戏历史记录: {game_history}")
    else:
        while guess != number_to_guess:
            guess = int(input(f"{player_name}, 请猜一个1到100之间的数: "))
            attempts += 1
            game_history.append((guess, attempts))
            if guess < number_to_guess:
                print("太低了!再试一次。")
            elif guess > number_to_guess:
                print("太高了!再试一次。")
        print(f"恭喜你,{player_name},你猜对了!这个数就是 {number_to_guess}。")
        print(f"你一共尝试了 {attempts} 次。")
        game_history.append((number_to_guess, attempts))
        print(f"你的游戏历史记录: {game_history}")
        play_again = input("你想再玩一次吗? (是/否): ")
        if play_again.lower() == "是":
            game()
        else:
            print("好的,谢谢参与!")
            with open('game_history.txt', 'w') as file:
                for item in game_history:
                    file.write(f"{item[0]},{item[1]}\n")
            print(f"游戏历史记录已保存到 'game_history.txt' 文件中.")
            exit()                    
相关推荐
sheji34168 分钟前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
木井巳14 分钟前
【递归算法】子集
java·算法·leetcode·决策树·深度优先
Zzzz_my1 小时前
正则表达式(RE)
pytorch·python·正则表达式
行百里er1 小时前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·代码规范
天天鸭1 小时前
前端仔写了个 AI Agent,才发现大模型只干了 10% 的活
前端·python·ai编程
ms_27_data_develop1 小时前
Java枚举类、异常、常用类
java·开发语言
xiaohe072 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
代码飞天2 小时前
wireshark的高级使用
android·java·wireshark
setmoon2142 小时前
使用Scikit-learn构建你的第一个机器学习模型
jvm·数据库·python