写一个函数,处理(numhands,n = 5,deck),那个 每张n张牌可以交易numhands牌。
import random # this will be a useful library for shuffling
from random import shuffle
# This builds a deck of 52 cards. If you are unfamiliar with this
# notation, check out Andy's supplemental video
# on list comprehensions (you can find the link in the
# Instructor Comments box below).
mydeck = [r+s for r in '23456789TJQKA' for s in 'SHDC']
def deal(numhands, n=5, deck=mydeck):
mynew = shuffle(deck)
if numhands*n > len(deck):
raise Exception('Not enough cards.')
hands = []
for i in range(0,numhands):
ncards = []
for j in range(0,n):
ncards.append(mynew.pop())
hands.append(ncards)
return hands
print deal(2)
我不确定该功能有什么问题,但它一直告诉我这个错误 在交易中 ncards.append(mynew.pop()) AttributeError:'NoneType'对象没有属性'pop'
答案 0 :(得分:3)
random.shuffle
随机播放
它改变了原始套牌......它不会返回任何内容
所以只需使用deck