新手小白写了个随机数抽奖的程序,请大佬指正
import random as rddef num(s):
while True:
try:
i = int(input(s))
if i< 0:
print('请重新输入一个正整数。')
continue
else:
break
except ValueError:
print('请重新输入一个正整数。')
continue
return i
sum_num = num('请输入抽奖池数量:')
luck_num = 0
round_num = num('请输入抽奖次数:')
while True:
if round_num > sum_num - luck_num:
print('中奖次数不合理,请重新输入抽奖次数。')
round_num = num('请输入抽奖次数:')
continue
else:
break
i = 0
lucks =[]
while i < round_num:
round_luck = []
j = 0
luck_num = num('请输入中奖人数:')
while True:
if luck_num > sum_num - len(lucks):
print(f'还剩{sum_num - len(lucks)}没有中奖,请重新输入中奖人数。')
luck_num = num('请输入中奖人数:')
continue
else:
break
while j < luck_num:
temp = rd.choice(range(1,sum_num+1))
if temp in lucks:
print(temp,'重复了!')
continue
else:
lucks.append(temp)
round_luck.append(temp)
print(f'第{j+1}个中奖者是',':',temp)
j += 1
print(f'第{i+1}次中奖者{len(round_luck)}位是:',round_luck)
i += 1
unlucks = []
for k in range(1,sum_num+1):
if k not in lucks:
unlucks.append(k)
unlucks.sort()
lucks.sort()
print(f'得奖人数是{len(lucks)},分别为:',lucks)
print(f'没有得奖人数是{len(unlucks)},分别为:',unlucks) 个人觉得纯练习挺好,但如果要做到现实可用至少应该做到界面化并且能互动才行,比如设置好后鼠标点一下,开始抽奖,再点一下停止之类。 random生成伪随机数,考虑numpy.random.choice 有随机选取元素的现成方法,或者打乱顺序使用pop抽奖
页:
[1]