无法打印返回的字符串 class
Can't print string from returned class
所以我想开发基本代码,但我被困在这里。我知道我必须调用 class Bootcamp 中的函数,但不知道如何调用。当输入作为训练营时,我想打印 class 训练营中的内容。帮助表示赞赏。
class match_start:
def choose_land(self):
print('You are in the plane. CHoose where to land')
land_area = input('->')
if land_area == 'bootcamp':
return 'Bootcamp'
elif land_area == 'docks':
return 'Docks'
else:
print('landed with bots')
class Bootcamp:
def bootcamp(self):
print ('Bootcamp it is')
class Docks:
def docks(self):
print('Docks it is')
x = match_start()
x.choose_land()
Output --
You are in the plane. CHoose where to land
->bootcamp
PS C:\Users\User\Downloads\New folder (2)\Practice>
PS - 我是编码初学者,正在学习 学习 Python 艰苦的方法 by Zed Shaw 所以请提出任何改进我的编码的建议。这也是我关于 Whosebug 的第一个问题,也避免了愚蠢的 pubg 参考。
所以,您似乎正试图同时做很多事情:
首先,为了打印选定的字符串,您只需将其处理到 print
函数,此代码的最简单版本如下所示(没有额外的 classes ):
class match_start:
def choose_land(self):
print('You are in the plane. CHoose where to land')
land_area = input('->')
if land_area == 'bootcamp':
print('Bootcamp it is')
elif land_area == 'docks':
print('Docks it is')
else:
print('landed with bots')
您似乎在尝试为每个目的地使用 class,这对于打印等基本功能来说有点混乱,为了让它在那里工作,您需要首先实例化class 然后调用它的位置函数,我不推荐它,但作为一个例子,你有:
class match_start:
def choose_land(self):
print('You are in the plane. CHoose where to land')
land_area = input('->')
destination = Destination(land_area)
destination.print_message()
class Destination(object):
def __init__(self, dest_name):
self.dest_name = dest_name
def print_message(self):
print (self.dest_name' + it is')
尝试熟悉只有函数的流程,然后 classes 会更容易理解。
我对您的代码进行了一些调整,并使用基本继承来实现您的目标。
希望这就是您要找的!您应在 LandArea class 中添加任意数量的位置,并在子 class Start_PubG.
中引用它们
class LandArea:
def bootcamp(self):
print ('You are landed in Bootcamp!')
def docks(self):
print('You are landed in Docks!')
class Start_PubG(LandArea):
def choose_land(self):
print('You are in the plane. Choose where to land!')
land_area = input('->')
if land_area.lower() == 'bootcamp':
super().bootcamp()
elif land_area.lower() == 'docks':
super().docks()
else:
print('Landed with bots')
obj = Start_PubG()
obj.choose_land()
***** 在您的评论后添加:******
嘿,你的方案应该使用与上述相同的方法来实现,但如你坚持的那样有 2 个不同的 classes。下面是相同的代码,
class Bootcamp:
def bootcamp(self):
print ('You are landed in Bootcamp!')
class Docks:
def docks(self):
print('You are landed in Docks!')
class Start_PubG(Bootcamp, Docks):
def choose_land(self):
print('You are in the plane. Choose where to land!')
land_area = input('->')
if land_area.lower() == 'bootcamp':
super().bootcamp()
elif land_area.lower() == 'docks':
super().docks()
else:
print('Landed with bots')
obj = Start_PubG()
obj.choose_land()
我也猜想,您想将用户输入转换为 Python 对象引用。如果是这样,您应该使用 eval() 函数来实现您的目标。以下是相同的方法。但是请确保用户提供的输入是区分大小写的并且与 class 名称保持一致,因为字符串直接转换为 python 对象并被调用,所以当一个不存在的 python调用此代码的对象将抛出错误。 [与以前的方法不同,这种方法不能在不区分大小写的情况下处理]
class Bootcamp:
def bootcamp(self):
print ('You are landed in Bootcamp!')
class Docks:
def docks(self):
print('You are landed in Docks!')
class Start_PubG(Bootcamp, Docks):
def choose_land(self):
print('You are in the plane. Choose where to land!')
land_area = input('->')
if land_area == 'Bootcamp':
obj_ref = eval(land_area)
obj_ref().bootcamp()
elif land_area == 'Docks':
obj_ref = eval(land_area)
obj_ref().docks()
else:
print('Landed with bots')
obj = Start_PubG()
obj.choose_land()
所以我想开发基本代码,但我被困在这里。我知道我必须调用 class Bootcamp 中的函数,但不知道如何调用。当输入作为训练营时,我想打印 class 训练营中的内容。帮助表示赞赏。
class match_start:
def choose_land(self):
print('You are in the plane. CHoose where to land')
land_area = input('->')
if land_area == 'bootcamp':
return 'Bootcamp'
elif land_area == 'docks':
return 'Docks'
else:
print('landed with bots')
class Bootcamp:
def bootcamp(self):
print ('Bootcamp it is')
class Docks:
def docks(self):
print('Docks it is')
x = match_start()
x.choose_land()
Output --
You are in the plane. CHoose where to land
->bootcamp
PS C:\Users\User\Downloads\New folder (2)\Practice>
PS - 我是编码初学者,正在学习 学习 Python 艰苦的方法 by Zed Shaw 所以请提出任何改进我的编码的建议。这也是我关于 Whosebug 的第一个问题,也避免了愚蠢的 pubg 参考。
所以,您似乎正试图同时做很多事情:
首先,为了打印选定的字符串,您只需将其处理到 print
函数,此代码的最简单版本如下所示(没有额外的 classes ):
class match_start:
def choose_land(self):
print('You are in the plane. CHoose where to land')
land_area = input('->')
if land_area == 'bootcamp':
print('Bootcamp it is')
elif land_area == 'docks':
print('Docks it is')
else:
print('landed with bots')
您似乎在尝试为每个目的地使用 class,这对于打印等基本功能来说有点混乱,为了让它在那里工作,您需要首先实例化class 然后调用它的位置函数,我不推荐它,但作为一个例子,你有:
class match_start:
def choose_land(self):
print('You are in the plane. CHoose where to land')
land_area = input('->')
destination = Destination(land_area)
destination.print_message()
class Destination(object):
def __init__(self, dest_name):
self.dest_name = dest_name
def print_message(self):
print (self.dest_name' + it is')
尝试熟悉只有函数的流程,然后 classes 会更容易理解。
我对您的代码进行了一些调整,并使用基本继承来实现您的目标。
希望这就是您要找的!您应在 LandArea class 中添加任意数量的位置,并在子 class Start_PubG.
中引用它们class LandArea:
def bootcamp(self):
print ('You are landed in Bootcamp!')
def docks(self):
print('You are landed in Docks!')
class Start_PubG(LandArea):
def choose_land(self):
print('You are in the plane. Choose where to land!')
land_area = input('->')
if land_area.lower() == 'bootcamp':
super().bootcamp()
elif land_area.lower() == 'docks':
super().docks()
else:
print('Landed with bots')
obj = Start_PubG()
obj.choose_land()
***** 在您的评论后添加:******
嘿,你的方案应该使用与上述相同的方法来实现,但如你坚持的那样有 2 个不同的 classes。下面是相同的代码,
class Bootcamp:
def bootcamp(self):
print ('You are landed in Bootcamp!')
class Docks:
def docks(self):
print('You are landed in Docks!')
class Start_PubG(Bootcamp, Docks):
def choose_land(self):
print('You are in the plane. Choose where to land!')
land_area = input('->')
if land_area.lower() == 'bootcamp':
super().bootcamp()
elif land_area.lower() == 'docks':
super().docks()
else:
print('Landed with bots')
obj = Start_PubG()
obj.choose_land()
我也猜想,您想将用户输入转换为 Python 对象引用。如果是这样,您应该使用 eval() 函数来实现您的目标。以下是相同的方法。但是请确保用户提供的输入是区分大小写的并且与 class 名称保持一致,因为字符串直接转换为 python 对象并被调用,所以当一个不存在的 python调用此代码的对象将抛出错误。 [与以前的方法不同,这种方法不能在不区分大小写的情况下处理]
class Bootcamp:
def bootcamp(self):
print ('You are landed in Bootcamp!')
class Docks:
def docks(self):
print('You are landed in Docks!')
class Start_PubG(Bootcamp, Docks):
def choose_land(self):
print('You are in the plane. Choose where to land!')
land_area = input('->')
if land_area == 'Bootcamp':
obj_ref = eval(land_area)
obj_ref().bootcamp()
elif land_area == 'Docks':
obj_ref = eval(land_area)
obj_ref().docks()
else:
print('Landed with bots')
obj = Start_PubG()
obj.choose_land()