使用python编写一套关于抽奖的代码
下面是一个简单的抽奖代码的示例:
import random
def lottery(participants):
if not participants:
print("没有参与者!")
return
winner = random.choice(participants)
print("恭喜抽奖获胜者:", winner)
participants = ["张三", "李四", "王五", "赵六", "钱七"]
lottery(participants)
这个代码中,lottery函数接受一个参与者列表作为参数。首先,它检查参与者列表是否为空,如果为空,则打印"没有参与者!"并返回。然后,它使用random.choice函数从参与者列表中随机选择一个获胜者,并打印出来。
你可以根据自己的需求修改和扩展这个代码,例如添加更多的抽奖逻辑、限制每个参与者的抽奖次数等等。
原文地址: https://www.cveoy.top/t/topic/iQyr 著作权归作者所有。请勿转载和采集!