Python功能卡d

2024-07-05 14:54:56 发布

您现在位置:Python中文网/ 问答频道 /正文

写一个函数,deal(numhands,n=5,deck),它 每手牌有n张牌。在

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(我的新.pop()) AttributeError:“NoneType”对象没有属性“pop”


Tags: 函数inimportforrandomthispopcards