将for循环放入函数的问题(Python3)

The*_*tro 3 python python-3.x

这篇文章已经回复,如果你愿意,请阅读.

我一直在尝试在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('>')
Run Code Online (Sandbox Code Playgroud)

编辑:没有收到任何错误,它只是关闭.

Att*_*son 5

UnboundLocalError:赋值前引用的局部变量'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
Run Code Online (Sandbox Code Playgroud)

这有效.