-1

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!

  • 1
    What do you mean the return to the previous part? You can call the function itself from within itself, would that do? So if the user choose 3 just call the same function again – SiP Dec 19 '22 at 08:30

2 Answers2

3

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

See: https://www.geeksforgeeks.org/returning-a-function-from-a-function-python/

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()
コリン
  • 1,005
  • 1
  • 5
  • 26
  • To return to the previous part i need to add that statement to make it work? If for example i want the question to branch `if __name__ == '__main__':` I just need to add that to the code? – TheBotHunter Dec 19 '22 at 08:40
  • You actaully don't need ```if __name__ == '__main__':``` statement. Simply calling the function ```Opening()``` will work fine. Feel free to ignore it or remove it. – コリン Dec 19 '22 at 08:45
  • It's just a good practice to have it when importing this module to another Python file. It ensures that anything within this statement will only execute when running the current Python file. – コリン Dec 19 '22 at 08:48
  • Hmm, i'm not sure that i understand it, correct me if i'm wrong. `if __name__ == '__main__':` so this command is used when you're trying to run this code in another python file? – TheBotHunter Dec 19 '22 at 08:58
  • When running file 1, file 1 has this statement ```__name__ == '__main__'``` to be true, so function ```Opening()``` will be excuted; however, if you import file 1 to file 2, ```__name__ == '__main__'``` (which is inside file 1) will become false in the context of file 2. Function ```Opening()``` will not be executed. Let's say you don't have this statement in file 1. Then when file 1 is imported to file 2, the function ```Opening()``` will be executed immediately, and you probably don't want this to happen... – コリン Dec 19 '22 at 09:05
  • Ahhh i think i'm starting to understand what you meant, so that line is used to make sure that the code in file 2 wouldn't run immediately? but what i don't understand is how it would immediately run if i don't add that line on file 2. – TheBotHunter Dec 19 '22 at 09:12
  • Here is a much better answer: https://stackoverflow.com/questions/419163/what-does-if-name-main-do – コリン Dec 19 '22 at 09:14
0

it works fine for me... maybe you can try to call the function recursively instead of using while true?

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

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

user_input = ''

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

    if user_input == '1':
        Twitter()
    elif user_input == '2':
        print('Instagram!')
        Opening()
    else:
        print('Type a number 1-2')
        Opening()

Twitter()
Bomragon z
  • 11
  • 3