0

所以我想开发基本代码,但我被困在这里。我知道我必须在 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 Shaw艰难地学习 Python 的初学者,所以请提出任何改进我的编码的建议。这也是我关于 stackoverflow 的第一个问题,也避免了愚蠢的 pubg 参考。

4

2 回答 2

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()
于 2020-05-08T15:30:35.893 回答
0

所以,看起来你正在尝试一次做很多事情:

首先,为了打印选定的字符串,您只需将其处理到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')

尝试熟悉仅包含函数的流程,然后这些类将更易于理解。

于 2020-05-08T15:25:54.463 回答