简体   繁体   English

调用类时局部变量引用错误

[英]Local variable referenced error when calling class

Originally I had 2 files, one named "cards" and one named "decks".最初我有 2 个文件,一个名为“cards”,一个名为“decks”。 The cards file contain the definition of the cards class and was imported into the "decks" file.卡片文件包含卡片类的定义并被导入到“decks”文件中。 In the decks files was the definition of the deck object.在甲板文件中是甲板对象的定义。 After defining the deck object, I would test the class in the lines below by typing something like "deck = deck()" and everything would work.在定义了deck 对象之后,我将通过键入诸如“deck =deck()”之类的内容来测试下面几行中的类,并且一切正常。

After verifying everything, I wanted to move the deck definition into the "cards" file, to create a library that would contain both the "card" and "deck" class definitions.在验证完所有内容后,我想将牌组定义移动到“卡片”文件中,以创建一个包含“卡片”和“牌组”类定义的库。 However after doing this, running "deck = deck()" failed, giving the following error.但是,在执行此操作后,运行“deck =deck()”失败,并出现以下错误。 This happens even if I run the "deck = deck()" line in the bottom of the cards file, or if I import cards an run in a separate file.即使我在卡片文件底部运行“deck =deck()”行,或者如果我在单独的文件中导入卡片,也会发生这种情况。

"card = card(name = name_of_card,suit = card_suit,value = 0) UnboundLocalError: local variable 'card' referenced before assignment" “card = card(name = name_of_card,suit = card_suit,value = 0) UnboundLocalError:赋值前引用了局部变量‘card’”

Below is the cards file code:下面是卡片文件代码:

import random


class card:
    def __init__(self, name, suit, value):
        self.name = name
        self.suit = suit
        self.value = value

class deck:
    def __init__(self, contents = [], num_cards = 0):
        self.contents = contents
        self.num_cards = num_cards
        self.generate()

    def generate(self):
        card_values = ['Ace', *range(2,11,1),'Jack', 'Queen', 'King']
        suits = ['Hearts','Clubs','Diamonds','Spades']
        for i in suits: 
            card_suit = str(i)
            for n in card_values:
                name_of_card = str(n) + " of " + card_suit
                card = card(name = name_of_card,suit = card_suit,value = 0)
                self.contents.append(card)
                self.num_cards += 1
    def shuffle(self):
        random.shuffle(self.contents)

    def print_contents(self):
        for i in self.contents:
            print(i.name)
    
    def draw_card(self):
        card_drawn = self.contents[0]
        self.contents.pop(0)
        return card_drawn

    def reveal(self,num_to_reveal = 1,from_top = True):
        
            for i in range(0,num_to_reveal,1):
                if from_top == True:
                    print(self.contents[i].name)
                else:
                    print(self.contents[-1-i].name)

    def return_to_deck(self,card, position = 0):
        self.contents.insert(position,card)

deck = deck()

You're using the same name for the class card and for the object you create for it.您对类card和为其创建的对象使用相同的名称。 Your code will work if you use different names.如果您使用不同的名称,您的代码将起作用。 Typically classes have names starting with a capitol letter, so I'd suggest class Card and then later, card = Card(name=name_of_card,suit=card_suit,value = 0) .通常,类的名称以大写字母开头,因此我建议使用class Card ,然后再使用card = Card(name=name_of_card,suit=card_suit,value = 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用 function 时出现“UnboundLocalError:赋值前引用的局部变量” - "UnboundLocalError: local variable referenced before assignment" when calling a function 赋值前引用的局部变量 Class & Module - Local Variable referenced before assignment Class & Module 在类内部赋值之前引用的局部变量 - Local Variable referenced before assignment inside of a class 声明与导入的类相同的变量时出现“赋值之前引用的局部变量”错误 - “Local variable referenced before assignment” error while declaring variable same as the imported class Python:当变量和class同名时:UnboundLocalError: local variable 'foo' referenced before assignment - Python: When variable and class have the same name: UnboundLocalError: local variable 'foo' referenced before assignment Python中赋值错误之前引用的局部变量 - Local variable referenced before assignment error in Python 分配Django错误之前引用的局部变量 - local variable referenced before assignment django error 未绑定错误,分配前已引用局部变量 - Unbound error, local variable referenced before assignment 在赋值错误python之前引用的局部变量 - Local variable referenced before assignment error python 分配错误之前引用的 Python 局部变量 - Python Local Variable Referenced Before Assignment Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM