Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i return to the previous question in Python Script

i’m new on using Python Script, i’ve been using Jupyter Notebook and finally realized the differences between the two of them. In this code i’m trying to create a simple command using def, where i need to type the number 1 – 3.

def Twitter():
    while True:
        user_input = input(
            'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')

        if user_input == '1':
            print('You picked Crawling')
            break
        elif user_input == '2':
            print('You picked Verification')
            break
        elif user_input == '3':
            print('Return')
            Opening()
            break
        else:
            print('Type a number 1-3')
            continue

user_input = ''

def Opening():
    while True:
        user_input = input(
            'Pick one: 1) Twitter | 2) Instagram ---->')
    
        if user_input == '1':
            Twitter()
            break
        elif user_input == '2':
            print('Instagram!')
            break
        else:
            print('Type a number 1-2')
            continue

I’m still very immature and I need help on when I picked the number 3, the function would return to the previous part instead of not outputing the result at all thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of using a break statement, you can return the function inside a function.

def Twitter():
    while True:
        user_input = input(
            'Pick one: 1) Crawling | 2) Verification | 3) Return ---->')

        if user_input == '1':
            print('You picked Crawling')
            break
        elif user_input == '2':
            print('You picked Verification')
            break
        elif user_input == '3':
            print('Return')
            return Opening()  # switch to Opening()
        else:
            print('Type a number 1-3')
            continue


def Opening():
    while True:
        user_input = input(
            'Pick one: 1) Twitter | 2) Instagram ---->')

        if user_input == '1':
            return Twitter()  # switch to Twitter()
        elif user_input == '2':
            print('Instagram!')
            break
        else:
            print('Type a number 1-2')
            continue


if __name__ == '__main__':
    Opening()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading