一个非常简易的抽奖脚本,大牛请绕行。python版本:3.8
v1.0是不自动剔除已经中奖的人名,适合总人数多而中奖人数少的情况。
v2.0会自动剔除已经中奖的人名并再次循环,适合大部分人员都会中奖的情况。
# -*- coding:utf-8 -*-
"""
简易年会抽奖脚本。
将同事人名单录入到脚本同路径下的NameList.txt,每个人名需换行,运行程序选出随机抽到的人名。
中奖一次的会记录到LuckyList.txt,再次中奖会提示。
v1.0 2023.1.5
"""
import time
import random
import os.path
author = 'Ditto2008'
# 检查脚本当前路径下是否包含已中奖名单文件,没有则创建。
if not os.path.isfile('LuckyList.txt'):
file = open('LuckyList.txt', mode='w')
file.close()
else:
pass
# 接收一个名单list,返回随机抽到的人名
def choose_luck_baby(namelist):
lucky_baby = ''
try:
lucky_baby = random.choice(namelist)
except ValueError:
print('姓名名单错误')
return lucky_baby
def main():
print('提前祝大家新年快乐!')
time.sleep(1)
print('谁会是这个幸运的小可爱呢~')
time.sleep(1)
print('随机抽奖倒计时开始...')
time.sleep(1)
print('......3......')
time.sleep(1)
print('......2......')
time.sleep(1)
print('......1......')
time.sleep(1)
with open('LuckyList.txt', mode='r', encoding='utf-8') as f:
luck_babies = f.readlines()
# print(luck_babies)
with open('NameList.txt', mode='r', encoding='utf-8') as f:
namelist = f.readlines()
# print(namelist)
new_lucky_baby = choose_luck_baby(namelist)
# print(lucky_baby)
lucky_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
# 检查是否已经中过奖
if new_lucky_baby not in luck_babies:
print('恭喜:', new_lucky_baby.strip())
print('中奖时间:', lucky_time)
with open('LuckyList.txt', mode='a', encoding='utf-8') as ff:
ff.write(new_lucky_baby.strip() + '\n')
else:
print('咦?又是' + str(new_lucky_baby.strip()) + '!' '真是运气爆棚,不过年会还是讲究个雨露均沾,再来一次吧^_^')
if __name__ == '__main__':
main()
名单文件NameList.txt应该是如下这种:
第一次中奖截图:
再次中奖截图:
# -*- coding:utf-8 -*-
"""
简易年会抽奖脚本。
将同事人名单录入到脚本同路径下的NameList.txt,每个人名需换行,运行程序选出随机抽到的人名。
中奖一次的会记录到LuckyList.txt,再次中奖会提示。
v2.0 2023.1.11 更改中奖逻辑,如果已中奖,直接再次抽奖。
"""
import time
import random
import os.path
author = 'Ditto2008'
# 检查脚本当前路径下是否包含已中奖名单文件,没有则创建。
if not os.path.isfile('LuckyList.txt'):
file = open('LuckyList.txt', mode='w')
file.close()
else:
pass
# 接收一个名单list,返回随机抽到的人名
def choose_luck_baby(namelist):
lucky_baby = ''
try:
lucky_baby = random.choice(namelist)
except ValueError:
print('姓名名单错误')
return lucky_baby
def main():
print('提前祝大家新年快乐!')
time.sleep(1)
print('谁会是这个幸运的小可爱呢~')
time.sleep(1)
print('随机抽奖倒计时开始...')
time.sleep(1)
print('......3......')
time.sleep(1)
print('......2......')
time.sleep(1)
print('......1......')
time.sleep(1)
with open('LuckyList.txt', mode='r', encoding='utf-8') as f:
luck_babies = f.readlines()
# print(luck_babies)
with open('NameList.txt', mode='r', encoding='utf-8') as f:
namelist = f.readlines()
# print(namelist)
new_lucky_baby = choose_luck_baby(namelist)
# print(lucky_baby)
lucky_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
# 检查是否已经中过奖
while len(luck_babies) < len(namelist):
if new_lucky_baby in luck_babies:
new_lucky_baby = choose_luck_baby(namelist)
continue
else:
print('恭喜:', new_lucky_baby.strip())
print('中奖时间:', lucky_time)
with open('LuckyList.txt', mode='a', encoding='utf-8') as ff:
ff.write(new_lucky_baby.strip() + '\n')
break
# 中奖名单长度等于名单长度的时候说明都已经中过奖。
else:
print('咦?似乎本次抽奖已是雨露均沾,再次祝大家新春快乐!')
if __name__ == '__main__':
main()