UID197841性别保密经验 EP铁粒 粒回帖0主题精华在线时间 小时注册时间2021-12-19最后登录1970-1-1
| 本帖最后由 用户名.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[n] in WinnersList:
- n=random.randint(0, UserNumber-1)
- WinnersList.append(UserList[n])
- winners=""
- for AwardName,prize,WinnersNumber in zip(AwardNameList,PrizeList,WinnersNumberList): #输出结果
- for i in range(WinnersNumber):
- winners+=WinnersList[0]+" "
- WinnersList.remove(WinnersList[0])
- 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[n] in NewUserList:
- n=random.randint(0, UserNumber-1)
- NewUserList.append(UserList[n])
- 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)
复制代码
运行结果:
|
评分查看全部评分
|