两种Python抽奖程序
本帖最后由 用户名.txt 于 2023-11-5 19:14 编辑一
源代码:
import random
AwardName=""
prize=""
WinnersNumber=0
AwardNameList=[]
PrizeList=[]
WinnersNumberList=[]
while AwardName!="q": #输入奖项名称、奖品以及中奖人数,直到输入奖项名称为“q”时停止
AwardName=input("请输入奖项名称:")
if AwardName=="q":
break
prize=input("请输入奖品:")
WinnersNumber=int(input("请输入中奖人数:"))
AwardNameList.append(AwardName)
PrizeList.append(prize)
WinnersNumberList.append(int(WinnersNumber))
TotalWinnersNumber=0
for WinnersNumber in WinnersNumberList: #获取中奖的总人数
TotalWinnersNumber+=WinnersNumber
UserName=""
UserList=[]
while UserName!="q": #输入用户名,直到输入“q”时停止
UserName=input("请输入用户名:")
if UserName=="q":
break
UserList.append(UserName)
UserNumber=len(UserList) #获取总人数
WinnersList=[]
for i in range(TotalWinnersNumber): #抽奖
n=random.randint(0, UserNumber-1)
while UserList in WinnersList:
n=random.randint(0, UserNumber-1)
WinnersList.append(UserList)
winners=""
for AwardName,prize,WinnersNumber in zip(AwardNameList,PrizeList,WinnersNumberList): #输出结果
for i in range(WinnersNumber):
winners+=WinnersList+" "
WinnersList.remove(WinnersList)
print(AwardName+":"+winners+","+"奖品:"+prize)
winners=""
运行结果:
二
这个程序的思路来源于https://klpbbs.com/thread-112364-1-1.html
源代码:
import random
BonusPool=int(input("请输入奖池金额:"))
UserName=""
UserList=[]
while UserName!="q": #输入用户名,直到输入“q”时停止
UserName=input("请输入用户名:")
if UserName=="q":
break
UserList.append(UserName)
UserNumber=len(UserList) #获取总人数
NewUserList=[]
while len(NewUserList)!=UserNumber: #排序
n=random.randint(0,UserNumber-1)
while UserList in NewUserList:
n=random.randint(0, UserNumber-1)
NewUserList.append(UserList)
BonusList=[]
n=0
for i in range(UserNumber): #抽奖
n+=1
if BonusPool!=0:
if n!=UserNumber:
bonus=random.randint(1,BonusPool)
BonusList.append(bonus)
BonusPool-=bonus
else:
BonusList.append(BonusPool)
else:
BonusList.append(0)
n=0
for UserName,bonus in zip(NewUserList,BonusList): #输出结果
n+=1
print(str(n)+".",UserName,bonus)
运行结果:
本帖最后由 用户名.txt 于 2024-3-25 22:55 编辑
2024.3.25补充
重写了这两个程序
import random
# 获取奖项名称和中奖人数
prize_name_list = []
winner_number_list = []
while True:
prize_name = input("请输入奖项名称:")
if prize_name == "q":
break
winner_number = int(input("请输入中奖人数:"))
prize_name_list.append(prize_name)
winner_number_list.append(winner_number)
# 获取用户列表
user_list = []
while True:
user_name = input("请输入用户名:")
if user_name == "q":
break
user_list.append(user_name)
# 打乱用户列表
random.shuffle(user_list)
# 根据打乱后的用户列表生成获奖用户名单
winner_list = []
for winner_number in winner_number_list:
winner_list.append()
# 输出结果
for prize_name, winner_names in zip(prize_name_list, winner_list):
winner_name = "、".join(winner_names)
print(f"{prize_name}:{winner_name}"
import random
# 获取奖池金额
bonus_pool = int(input("请输入奖池金额:"))
# 获取用户列表
user_list = []
while True:
user_name = input("请输入用户名:")
if user_name == "q":
break
user_list.append(user_name)
# 打乱用户列表
random.shuffle(user_list)
# 抽奖
winning_list = {user_name: 0 for user_name in user_list}
for user_name in winning_list.keys():
if bonus_pool == 0:
break
bonus = random.randint(1, bonus_pool)
winning_list += bonus
bonus_pool -= bonus
# 输出结果
n = 0
for user_name, bonus in winning_list.items():
n += 1
print(f"{n}.{user_name}\t{bonus}") 感觉 Python 比 C++ 抽象(
好复杂啊,还是直接random.randint方便
页: [1]