如何在函数外使用这些变量 (python)?
How do I use these variables outside of the function (python)?
我最近回到 Python 编程,我有一个问题。在我的代码中,我正在尝试设置插槽,但我不知道如何打印它们。
我有两个文件,Card 和 Main。这是主文件的代码。
cardsList = ["dog", "fairy", "flower", 'ghost', "bow", "brain", "bird", "mountain", "glasses", "book", "water", "family", 'scream', "mask", "bacteria", "gun", "bomb", "motocycle"]
New = Card(cardsList)
New.Hand()
对于那个来说,这是不言自明的。这是卡片文件的代码。
class Card(object):
cardsList = list(["dog", "fairy", "flower", 'ghost', "bow", "brain", "bird", "mountain", "glasses", "book", "water", "family", 'scream', "mask", "bacteria", "gun", "bomb", "motocycle"])
slot1 = ""
slot2 = ""
slot3 = ""
slot4 = ""
slot5 = ""
slot6 = ""
slot7 = ""
slot8 = ""
slot9 = ""
def __init__(self, cardsList):
self.cardsList = cardsList
def __len__(self):
return len(self.cardsList)
def Hand(cardsList):
return len(cardsList)
cardsList = random.shuffle(cardsList)
listTwo = []
for i in range(9):
listTwo.append(cardsList[i])
cardsList.pop(i)
return listTwo
slot1 = listTwo[1]
slot2 = listTwo[2]
slot3 = listTwo[3]
slot4 = listTwo[4]
slot5 = listTwo[5]
slot6 = listTwo[6]
slot7 = listTwo[7]
slot8 = listTwo[8]
slot9 = listTwo[9]
slots = [slot1, slot2, slot3, slot4, slot5, slot6, slot7, slot8, slot9]
print(slots)
请告诉我如何在函数外定义插槽函数。我需要将插槽用于不同的功能,但我不知道如何让它们工作。
我想这就是你想要做的:
import random
class Card(object):
def __init__(self, cardsList):
self.cardsList = cardsList
self.slot1 = ""
self.slot2 = ""
self.slot3 = ""
self.slot4 = ""
self.slot5 = ""
self.slot6 = ""
self.slot7 = ""
self.slot8 = ""
self.slot9 = ""
def __len__(self):
return len(self.cardsList)
def Hand(self):
random.shuffle(self.cardsList)
self.slot1 = cardsList[0]
self.slot2 = cardsList[1]
self.slot3 = cardsList[2]
self.slot4 = cardsList[3]
self.slot5 = cardsList[4]
self.slot6 = cardsList[5]
self.slot7 = cardsList[6]
self.slot8 = cardsList[7]
self.slot9 = cardsList[8]
cardsList = ["dog", "fairy", "flower", 'ghost', "bow", "brain", "bird", "mountain", "glasses", "book", "water", "family", 'scream', "mask", "bacteria", "gun", "bomb", "motocycle"]
myCards = Card(cardsList)
myCards.Hand()
- 创建 class 的对象会将所有插槽初始化为“”。
- 调用对象的
Hand
函数将 cardsList 中的 9 张随机卡片分配到插槽 1 到 9。
顺便说一句,random.shuffle
将列表 就地 打乱,因此您不应该将其分配给任何东西。
我最近回到 Python 编程,我有一个问题。在我的代码中,我正在尝试设置插槽,但我不知道如何打印它们。
我有两个文件,Card 和 Main。这是主文件的代码。
cardsList = ["dog", "fairy", "flower", 'ghost', "bow", "brain", "bird", "mountain", "glasses", "book", "water", "family", 'scream', "mask", "bacteria", "gun", "bomb", "motocycle"]
New = Card(cardsList)
New.Hand()
对于那个来说,这是不言自明的。这是卡片文件的代码。
class Card(object):
cardsList = list(["dog", "fairy", "flower", 'ghost', "bow", "brain", "bird", "mountain", "glasses", "book", "water", "family", 'scream', "mask", "bacteria", "gun", "bomb", "motocycle"])
slot1 = ""
slot2 = ""
slot3 = ""
slot4 = ""
slot5 = ""
slot6 = ""
slot7 = ""
slot8 = ""
slot9 = ""
def __init__(self, cardsList):
self.cardsList = cardsList
def __len__(self):
return len(self.cardsList)
def Hand(cardsList):
return len(cardsList)
cardsList = random.shuffle(cardsList)
listTwo = []
for i in range(9):
listTwo.append(cardsList[i])
cardsList.pop(i)
return listTwo
slot1 = listTwo[1]
slot2 = listTwo[2]
slot3 = listTwo[3]
slot4 = listTwo[4]
slot5 = listTwo[5]
slot6 = listTwo[6]
slot7 = listTwo[7]
slot8 = listTwo[8]
slot9 = listTwo[9]
slots = [slot1, slot2, slot3, slot4, slot5, slot6, slot7, slot8, slot9]
print(slots)
请告诉我如何在函数外定义插槽函数。我需要将插槽用于不同的功能,但我不知道如何让它们工作。
我想这就是你想要做的:
import random
class Card(object):
def __init__(self, cardsList):
self.cardsList = cardsList
self.slot1 = ""
self.slot2 = ""
self.slot3 = ""
self.slot4 = ""
self.slot5 = ""
self.slot6 = ""
self.slot7 = ""
self.slot8 = ""
self.slot9 = ""
def __len__(self):
return len(self.cardsList)
def Hand(self):
random.shuffle(self.cardsList)
self.slot1 = cardsList[0]
self.slot2 = cardsList[1]
self.slot3 = cardsList[2]
self.slot4 = cardsList[3]
self.slot5 = cardsList[4]
self.slot6 = cardsList[5]
self.slot7 = cardsList[6]
self.slot8 = cardsList[7]
self.slot9 = cardsList[8]
cardsList = ["dog", "fairy", "flower", 'ghost', "bow", "brain", "bird", "mountain", "glasses", "book", "water", "family", 'scream', "mask", "bacteria", "gun", "bomb", "motocycle"]
myCards = Card(cardsList)
myCards.Hand()
- 创建 class 的对象会将所有插槽初始化为“”。
- 调用对象的
Hand
函数将 cardsList 中的 9 张随机卡片分配到插槽 1 到 9。
顺便说一句,random.shuffle
将列表 就地 打乱,因此您不应该将其分配给任何东西。