我正在尝试创建 Python 版本的大富翁。我有一个单独的类,用于洗牌和跟踪机会卡和公益金卡。这些卡片存储在列表 chest_cards
和 chance_cards
中。
def __init__(self):
self.chance = random.shuffle(chance_cards)
self.chest = random.shuffle(chest_cards)
self.chance_count = 0
self.chest_count = 0
def chance(self):
self.chance_count += 1
return self.chance[self.chance_count - 1]
在我的主代码中,我只是在运行
p = cards()
print (p.chance())
测试我的代码,但我得到打印行的TypeError: 'NoneType' object is not callable
。
有什么想法吗?或者您需要查看更多代码吗? TIA
编辑:这是完整的cards
类,如果有帮助的话
import random
global chance_count
global chest_count
class cards:
global chest_cards
global chance_cards
chest_cards = (["Go to Jail","Get Out of Jail Free","Advance to Go (Collect $200)",
"Bank error in your favor (Collect $200)","Doctor's fee (Pay $50)",
"From sale of stock you get $50", "Grand Opera Night — Collect $50 from every player",
"Holiday Fund matures (Collect $100)", "Income tax refund (Collect $20)",
"It is your birthday (Collect $10)","Life insurance matures (Collect $100)",
"Pay hospital fees of $100", "Pay school fees of $150", "Receive $25 consultancy fee",
"You are assessed for street repairs – $40 per house – $115 per hotel",
"You have won second prize in a beauty contest (Collect $10)", "You inherit $100"])
chance_cards = (["Go to Jail","Get Out of Jail Free","Advance to Go (Collect $200)",
"Advance to Illinois Ave — If you pass Go, collect $200",
"Advance to St. Charles Place – If you pass Go, collect $200",
"Advance token to nearest Utility. If unowned, you may buy it from the Bank. If owned, throw dice and pay owner a total ten times the amount thrown.",
"Advance token to the nearest Railroad and pay owner twice the rental to which he/she is otherwise entitled. If Railroad is unowned, you may buy it from the Bank.",
"Bank pays you dividend of $50", "Go Back 3 Spaces",
"Make general repairs on all your property – For each house pay $25 –For each hotel $100",
"Pay poor tax of $15","Take a trip to Reading Railroad – If you pass Go, collect $200",
"Take a walk on the Boardwalk – Advance token to Boardwalk",
"You have been elected Chairman of the Board – Pay each player $50",
"Your building and loan matures — Collect $150", "You have won a crossword competition (Collect $100)"])
def __init__(self):
self.chance = random.shuffle(chance_cards)
self.chest = random.shuffle(chest_cards)
self.chance_count = 0
self.chest_count = 0
def chance(self):
self.chance_count += 1
return self.chance[self.chance_count - 1]
最佳答案
当您创建类的实例时(假设它是 __init__
函数之前的 class cards:
),您可以使用名为 chance< 的方法创建一个对象
,但在 __init__
期间,您使用名为 chance
的属性覆盖此方法,其返回值 random.shuffle
始终为 None
因为 shuffle 就地“洗牌”列表,而不是按随机顺序创建新列表:
>>> chance_cards = ['card1', 'card2', 'card3']
>>> chance = random.shuffle(chance_cards)
>>> print(chance)
None
编辑:关于全局变量的注释
摆脱global
的选项(你真的应该自己做一些outside learning on variable scope...):
- 将类外部的变量移至“模块范围”中。您仍然可以在类里面引用它们。
import random, copy #copy isn't strictly needed but used for clarity
# `new_list=some_list[:]` is functionally equivalent to `new_list=copy.copy(some_list)`
chest_cards = (["Go to..."])
chance_cards = (["Go to Ja..."])
class cards:
def __init__(self):
self.chest_cards = copy.copy(chest_cards) #make a local copy so you don't alter the master list
random.shuffle(self.chest_cards)
- 如果您只在类中需要它们,请将它们保留为类属性,并使用
self
或类名称引用它们。
import random, copy
class cards:
chest_cards = (["Go to..."])
chance_cards = (["Go to Ja..."])
def __init__(self):
#before you over-write self.chest_cards, it refers to the class attribute
self.chest_cards = copy.copy(self.chest_cards) #make a local copy so you don't alter the master list
#after you over-write it, it will refer to the instance attribute as long as you made a copy.
#you can also refer to the class directly to access class-attributes
self.chest_cards = copy.copy(cards.chest_cards)
#if you want to get the class without calling it by name (in case you want to change the name of the class)
self.chest_cards = copy.copy(type(self).chest_cards)
random.shuffle(self.chest_cards)
- 肯定还有更多方法...看看你是否能找到:)
关于Python类: TypeError: 'NoneType' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65876961/