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()

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

*效果展示:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI自修室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值