python 练习题-扑克洗牌
1 ''' 2 题目: 一副扑克牌,13*4+2张,要求洗牌,也就是把牌尽量打乱 3 ''' 4 5 ''' 6 1.先生成1-5的随机数,对应四种花色和大小王,找到对应花色后, 7 再生成随机数取出对应的牌,然后删除对应的号码(以下程序使用此思路) 8 2.可以将54张牌编号,然后再随机生成54个随机数,递减 9 ''' 10 11 import random 12 # 四种颜色的扑克 13 flow = ['黑桃', '红桃', '方块', '梅花','王'] 14 15 # 号码,四种花色 16 numFlowBlack = [ '2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k','a'] 17 numFlowRed =numFlowBlack.copy() 18 numFlowBlock = numFlowBlack.copy() 19 numFlow = numFlowBlack.copy() 20 # 大小王 21 king = ['big', 'small'] 22 23 num = [numFlowBlack, numFlowRed, numFlowBlock, numFlow, king] 24 # 已经取出的扑克牌 25 pokeGet = [] 26 27 # 随机取值 28 def pokeGetRandom(): 29 while True: 30 # 随机生成四种花色和大小王的数字, 31 i = random.randint(1,len(num)) 32 33 # 从取出的花色中随机取值,然后删掉 34 if len(num[i-1]) > 0: 35 j = random.randint(0, len(num[i-1])-1) 36 pokeGet.append(flow[i - 1]+num[i-1][j]) 37 num[i-1].pop(j) 38 39 if len(num[0]) == 0 and len(num[1]) == 0 and len(num[2]) == 0 and len(num[3]) == 0 and len(num[4]) == 0 : 40 break 41 42 return pokeGet 43 44 print(pokeGetRandom())
问题:使用while 循环,在最后可能会出现多余的循环