• Python抽奖系统-----控制台显示


    以下是一个 Python 抽奖系统,其中包含更多的功能,如添加参与者、多轮抽奖、保存获奖者名单等:

    import random
    import os
    
    def load_participants():
        try:
            with open("participants.txt", "r") as file:
                participants = file.read().splitlines()
            return participants
        except FileNotFoundError:
            return []
    
    def save_participants(participants):
        with open("participants.txt", "w") as file:
            file.write("\n".join(participants))
    
    def draw_winner(participants):
        if not participants:
            print("没有足够的参与者来进行抽奖。请添加参与者后再试。")
            return
    
        winner = random.choice(participants)
        print("恭喜 {} 获奖!".format(winner))
        return winner
    
    def main():
        participants = load_participants()
    
        while True:
            print("\n抽奖系统菜单:")
            print("1. 添加参与者")
            print("2. 进行抽奖")
            print("3. 退出")
    
            choice = input("请选择操作 (1/2/3): ")
    
            if choice == "1":
                name = input("请输入参与者的名字: ")
                participants.append(name)
                save_participants(participants)
                print("{} 已添加到参与者列表。".format(name))
            elif choice == "2":
                winner = draw_winner(participants)
                if winner:
                    participants.remove(winner)
                    save_participants(participants)
            elif choice == "3":
                print("抽奖系统已退出。")
                break
            else:
                print("无效的选择,请重新输入。")
    
    if __name__ == "__main__":
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    这个抽奖系统允许你添加参与者、进行抽奖,抽奖结果会从参与者列表中移除,并且可以多次运行。抽奖系统会将参与者保存在一个名为 “participants.txt” 的文本文件中,以便下次使用。

    *效果展示:

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    Echarts X轴前面加文字
    程序的链接
    给指针初始化为0与nullptr
    初识深度学习-吴恩达
    RPC 实战与核心原理分析
    java.sql.SQLSyntaxErrorException: Unknown column ‘id‘ in ‘field list‘
    系统和系统实例-软件方法(下)第9章分析类图案例篇Part07
    CSDN21天学习挑战赛之折半查找
    Flink的状态持久化和状态后端
    python request post from 提交表单
  • 原文地址:https://blog.csdn.net/weixin_42367888/article/details/133854714