【问题标题】:Can't print string from returned class无法从返回的类中打印字符串
【发布时间】:2020-08-24 04:38:18
【问题描述】:

所以我想开发基本代码,但我被困在这里。我知道我必须在 Bootcamp 类中调用该函数,但不知道如何调用。当输入作为训练营给出时,我想打印类训练营中的内容。帮助表示赞赏。

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 - 我是编码和从 Zed ShawLearn Python the hard way 学习的初学者,所以请提出任何改进我的编码的建议。这也是我关于 stackoverflow 的第一个问题,也避免了愚蠢的 pubg 参考。

【问题讨论】:

  • 你既不打印x.choose_land()的结果,也不使用Bootcamp类。你为什么希望看到他们的字符串被打印出来?
  • 我猜我在land_area = input('->') if land_area == 'bootcamp': return 'Bootcamp'使用了Bootcamp类

标签: python-3.x string python-class pubg


【解决方案1】:

我对您的代码进行了一些调整,并使用基本继承来实现您的目标。

希望这就是您要找的东西!您应该在 LandArea 类中添加任意数量的位置,并在子类 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 个不同的类。以下是相同的代码,

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() 函数来实现您的目标。以下是相同的方法。但是请确保用户提供的输入区分大小写并且与类名保持不变,因为字符串直接转换为 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()

【讨论】:

  • 非常感谢它的帮助,但我需要为未来的训练营和停靠站创建单独的课程。那么如何为两者创建独立的类并在给出相应输入时打印其中的内容。
  • 嘿,我在答案中添加了几个场景。请检查。 @rajk1204
  • 感谢您的努力,这正是我想要的。欣赏它@Maheshkrishna AG
【解决方案2】:

所以,您似乎想同时做很多事情:

首先,为了打印所选字符串,您只需将其处理到print 函数,此代码的最简单版本将是这样的(没有额外的类):

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 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')

尝试熟悉仅包含函数的流程,然后类会更容易理解。

【讨论】:

  • 感谢您的帮助,但我看到了一个代码,其中使用 return ABC 返回了一个类,并猜测是否有办法返回该类并打印出来。
猜你喜欢
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 2021-03-16
相关资源
最近更新 更多