【发布时间】:2015-04-09 23:17:29
【问题描述】:
有人能解释一下为什么我在下面的类中的 enter 方法返回两个值吗?我正在学习 python 并正在创建一个简单的游戏来掌握 OOP 和类。无论如何,我需要 enter 方法从 sn-ps 列表中返回一个随机的 sn-p。但我不断得到两个 sn-ps 而不是一个。有人可以解释为什么吗?
from sys import exit
from random import randint
class Island(object):
def enter(self):
pass
class Loser(Island):
snippets = ["Welcome to loser Island",
"Can't win them all",
"There's always next time"]
def enter(self):
print Loser.snippets[randint(0,len(self.snippets)-1)]
loser_test = Loser()
loser_test.enter()
【问题讨论】:
-
这是您的完整代码吗?就像在这段代码中一样, Loser.enter 不返回任何内容;它只是打印一些东西。另外,您能否修复缩进以反映类结构?
-
你用过
print loser_test.enter()吗? -
@Kasra 他们不应该这样做。方法本身有
print语句 -
它不返回任何值 .... 所以我不相信你它返回两个 ...
-
上面的代码只为我打印了一件事,所以问题一定不在这里。
标签: python function class methods