1

So I want to print a string and then call a method, which is inside the class within the same line.

My code is following:

class A:
    
    def a(self):
        return 'bla bla\n' + A.b(self)
    
    def b(self):
        self.input = input('Type here:')
        if self.input == 'yes':
            return '\nok'
        else:
            return '\nnot ok'
    
d = A()

print(d.a())

The results I get:

Type here: (when I type 'Yes')

bla bla

ok

What I want is this:

bla bla

Type here: (when I type 'Yes')

ok

I want that 'bla bla' would be displayed first and then based on the input you would get either 'ok' or 'not ok'.

Can someone please help with what do I need to change?

2
  • 3
    Mixing concatenation of strings and printing directly will make this harder than it needs to be. b needs to be called before a can return, which means the input string will be printed first. I'd move the input-asking code out, pass the result into b, and have b make decisions based on already-received input. Commented Jul 1, 2021 at 14:08
  • 1
    it is completely business logic, not technical problem. Commented Jul 1, 2021 at 14:09

1 Answer 1

1

Change method a like -

def a(self):
    print('bla bla\n')
    return A.b(self)

The print statement will execute first and then return method b and asks you the input

Sign up to request clarification or add additional context in comments.

1 Comment

Great idea! Thanks! Don't know how I didn't come up with that (am quite new in Python).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.