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

时间:2018-06-14 22:24:05

标签: 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('>')

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

4 个答案:

答案 0 :(得分: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

这很有效。

答案 1 :(得分:4)

我不知道如何玩这个游戏,但我相信它的崩溃是由命名范围问题引起的(Python&#34;假设&#34;我们想要一个局部变量,因为赋值在里面计算of deal(),所以第一个print语句抛出此错误消息。在函数内部更改或创建的任何变量都是本地的,如果它没有被声明为全局变量),以下是有效的代码细

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):
    global count
    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('>')

有关命名范围的更多详情,请参阅https://www.programiz.com/python-programming/global-local-nonlocal-variables 要么 https://www.python-course.eu/global_vs_local_variables.php

答案 2 :(得分:1)

您的代码崩溃的原因是因为您未在交易函数中将计数定义为global,请阅读此thread以了解有关它的更多信息。

以下是您的代码的可能修复方法:

import random

print("Sam's Casino")
cards = ([str(i) for i in range(1, 10)] + ['J', 'Q', 'K']) * 4
random.shuffle(cards)
print(cards)
hand1 = []
hand2 = []
count = 0


def deal(cards):
    global count
    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('>')

尽管如此,一些建议尽量避免使用全局变量,并且使用明星运算符使用from random import *等语句来避免污染全局命名空间。这是对您的代码进行的一次小型重构:

import random


class Game:

    def __init__(self):
        self.cards = ([str(i) for i in range(1, 10)] + ['J', 'Q', 'K']) * 4
        self.count = 0
        self.hand1 = []
        self.hand2 = []

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

    def run(self):
        print("Sam's Casino")
        random.shuffle(self.cards)
        print(self.cards)
        self.deal()
        print(self.hand1)
        print(self.hand2)
        input('>')


if __name__ == "__main__":
    Game().run()

答案 3 :(得分:0)

另一种选择:

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,count):
    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,count)
print(hand1)
print(hand2)
input('>')

输出:

Sam's Casino
['1', '4', 'K', '5', '9', '3', 'J', '10', '8', '7', '8', 'J', 'Q', 'J', '5', '5', '10', 'K', '1', '8', '6', '9', '6', '5', '3', '1', 'Q', '2', '6', 'K', '10', '7', '1', 'Q', 'Q', '8', '7', '2', '3', '3', '6', '9', 'K', '7', 'J', '9', '2', '10', '4', '2', '4', '4']
['1', '4', 'K', '5']
['5', '9', '3', 'J']
>