Python 晚会抽奖程序

`import random

奖品列表

prizes = [‘A Prize’, ‘B Prize’, ‘C Prize’]

参与抽奖的人员列表

participants = [‘Alice’, ‘Bob’, ‘Charlie’, ‘Dave’, ‘Eve’]

每个人最多可以抽几次奖

max_attempts = 3

记录每个人抽奖的次数

attempts = {}

初始化每个人的抽奖次数

for participant in participants:
attempts[participant] = 0

进行抽奖,直到所有奖品都被抽完

while len(prizes) > 0:
# 随机抽取一名参与者
participant = random.choice(participants)

# 如果该参与者已经达到了最大抽奖次数,就跳过
if attempts[participant] >= max_attempts:
    continue

# 否则,随机抽取一件奖品
prize = random.choice(prizes)

# 把奖品从列表中删除
prizes.remove(prize)

# 打印获奖信息
print(f'{participant} won {prize}!')

# 增加该参与者的抽奖次数
attempts[participant] += 1

`@Python 晚会抽奖程序

你可能感兴趣的:(python)