这篇文章已经被回复了,如果你愿意的话可以读一下。
我一直试图用python 3编写一个纸牌游戏,我一直在使用for循环将纸牌从牌组列表转移到手牌列表我试图将其放入函数中,但命令提示符崩溃。帮忙?

from random import *
print("Sam's Casino")
cards = ['1','2','3','4','5','6','7','8','9','10','J',
'Q','K','1','2','3','4','5','6','7','8','9','10','J',
'Q','K','1','2','3','4','5','6','7','8','9','10','J',
'Q','K','1','2','3','4','5','6','7','8','9','10','J',
'Q','K']
shuffle(cards)
print(cards)
hand1 = []
hand2 = []
count = 0
def deal(cards):
    for card in cards:
        if count < 4:
            hand1.append(card)
            count += 1
        if count > 3 and count < 8:
            hand2.append(card)
            count += 1

deal(cards)
print(hand1)
print(hand2)
input('>')

编辑:没有收到错误,它只是关闭。

最佳答案

unbundlocalerror:赋值前引用的局部变量“count”
count放入函数deal

def deal(cards):
    count = 0
    for card in cards:
        if count < 4:
            hand1.append(card)
            count += 1
        if count > 3 and count < 8:
            hand2.append(card)
            count += 1

这很管用。

08-05 00:00