我试图设计一个基于简单选择的视频游戏。基本上我想要的是一个递归循环,它将继续根据前一个结果调用新的级别。例如,在基于级别1的关闭选择中,它将触发级别2或3.这是我到目前为止的代码:
class Levels:
def __init__(self, Next = 1):
self.Next = Next
def Call(self):
VarLevel = "Level" + "{}".format(self.Next)
return ("{}".format(VarLevel))
这是超类,它返回VarLevel,它等于Level1以触发子类Level1。这是我对关卡的代码,我已经排除了游戏的背景,因为我认为没有必要。
class Level1(Levels):
def __init__(self,):
# this just includes information about the level to show the user
# (objective etc.)
def Action(self):
# this will include the content of the level. based off the choices
# made I want it to return to the super with VarLevel as Level2 or
# Level3 and then to trigger the next level running and repeat
# throughout the program to run the game. For the purpose of testing
# the program the only content of Level1 is setting the return to 2 so
# that Level2 is called. I'm having trouble actually getting it to
# recognize my return and to trigger the next level. This is the
# actual method I want to do the calling with
class LevelCall():
def __init__(self, Levels = Levels):
self.Levels = Levels
def Calling(self):
result = (Levels, "{}".format(Levels()))()
它给了我错误TypeError: 'tuple' object is not callable
。我已经做了很多不同的尝试让它工作,所以我不确定这甚至是代码的真正问题。另外值得注意的是我在Java方面很不错,现在正在转向Python(这是我在Python中的第一次尝试,然后是读/写等基本测试。)非常感谢任何帮助,以帮助弄清楚如何格式化游戏,我道歉提前因为我知道这是一个很长的问题,我以前从未在这里发布过,所以如果您需要更多信息或说明,请随时提出。
编辑: 这是完整的错误消息
Traceback (most recent call last):
line 54, in <module>
Tester.Calling()
line 50, in Calling
result = (Levels, "{}".format(Levels()))()
TypeError: 'tuple' object is not callable
另一个编辑: 我想我越来越近了。我做了以下更改
class LevelCall():
def __init__(self, Levels = Levels):
self.Levels = Levels
def Calling(self):
Hold = Levels()
result = (getattr(Levels, "{}".format(Hold.Call()))())
现在提供以下错误消息。
Traceback (most recent call last):
line 55, in <module>
Tester.Calling()
line 51, in Calling
result = (getattr(Levels, "{}".format(Hold.Call()))())
AttributeError: type object 'Levels' has no attribute 'Level1'
如果我理解正确,它现在正在尝试做我想要的但是没有找到“Level1”类。再次感谢所有帮助。
编辑______________________ 我要感谢所有回复并试图提供帮助的人,我非常感谢你的支持。通过澄清,您可以帮助我以及今天重新开始,并首先将其映射到java中以使转换更容易,我能够解决我的问题。再次非常感谢你,我将添加我在此编辑下找到的解决方案。
global Stop
class Level1 :
def __init__(self):
self
def Action(self):
print ("1")
global Stop
Stop = input("Would you like to advance to the next level?")
if (Stop == "yes"):
# Lev = Level2()
# return Lev.Action()
return Level2
if (Stop == "no"):
return "stop"
class Level2:
def __init__(self):
self
def Action(self):
print("2")
global Stop
Stop = input("Would you like to advance to the next level?")
if (Stop == "yes"):
# Lev = Level3()
# return Lev.Action()
return Level3
if (Stop == "no"):
return "stop"
class Level3 :
def __init__(self):
self
def Action(self):
print ("3")
global Stop
Stop = input ("Next level??")
if (Stop == "yes"):
# Lev = Level4()
# return Lev.Action()
return Level4
if (Stop == "no"):
return "stop"
class Level4:
def __init__(self):
self
def Action(self):
print ("Complete")
return "Done"
def Runner (Level):
if (Level == "Done"):
print ("Bye")
else :
if (Level != "stop"):
Lev = Level()
Next = Lev.Action()
Runner(Next)
if (Level == "stop"):
print ("you chose to stop")
Runner(Level1)
答案 0 :(得分:0)
(a,b,c)
是元组语法。 (a,b,c)()
是一个被称为函数的元组。这就是错误所指的内容。
如果我们打破违规代码,你可以告诉我们。将format
的调用替换为arg
占位符后,它会是什么样子:
(Levels, "{}".format(Levels()))()
变成......
(Levels, arg)() # this is now clearly a tuple and you're treating it like a function.
不确定如何修复可以帮助你解决关卡问题。
如果您想调用某个功能,请执行以下操作:func(args)
如果要定义元组,请执行以下操作:(a, b, ..., z)
但是不要把元组称为函数。