Search code examples
python-3.xstringpython-class

Can't print string from returned class


So I wanted to develop the basic code but I'm stuck here. I know I have to call the function in class Bootcamp but don't know how. I want to print what's in class Bootcamp when input is given as bootcamp. Help appreciated.

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 - I'm beginner in coding and learning from Learn Python the hard way by Zed Shaw so pls suggest anything to improve my coding. Also this is my first question on stackoverflow and also avoid the dumb pubg reference.


Solution

  • I have made some tweaks to your code and used basic inheritance to achieve your goal.

    Hope this is what you are looking for! You shall add as many locations as you want in the LandArea class and refer them in the child 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()
    

    ***** Added after your comment: ******

    Hey, your scenario shall be achieved with the same approach as above but with 2 different classes as you insisted. Below is the code for the same,

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

    I also guess, you would like to convert the user input into a Python object reference. If so you shall use the eval() function to achieve your goal. Below is the approach for the same. But make sure the input provided by the user is Case Sensitive and intact with the class names, as the strings are directly converted to the python objects and called, so when a non existing python object is called this code will throw error. [Unlike the previous approaches, this approach can not be handled without case sensitive]

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