How do I put a close button on my python file?

Asked

Viewed 40 times

-3

I’m starting in python and I’m writing a code, but when all the actions end the program closes, how do I put a command where for example click ESC and it closes? I want that when the person does not type Youtube, she can write again, when closing.

ver = input()
if ver == 'Youtube' :
        print('Abrindo youtube...')

        import webbrowser

        webbrowser.open('https://www.youtube.com/', new=2)

else:

        print('Desculpe, não entendi')

1 answer

0

In this particular case, the way to treat the problem in a simple and objective way, in my view, would be to determine a function to receive the input in question.

It would look something like this: The function displays the input message to the user and receives the data. After passing the conditional and falling into Else, you could simply display a print stating that the typing was invalid and call the function again.


In code, it would look like this:

    import webbrowser
    
    def insere_URL():
        ver = input("Digite o aplicativo desejado: ")
    
        if ver == 'Youtube' :
                print('Abrindo youtube...')
                webbrowser.open('https://www.youtube.com/', new=2)
        else:
                print('Desculpe, não entendi. Tente novamente...')
                insere_URL()
    
    insere_URL()

Tips and observations: Always make your Imports right at the beginning of the program and watch out a little more for the indentation of your code. Try to skip line only when the next statement is not related to the one above, for example, you don’t need to skip line after an if or Else since what is just below is part of that block. However, if inside the block have codes that do different activities, it is worth a line pulation ^^

It is also interesting to display text inside input() for the user to know what to type.

!! IMPORTANT !!! Never forget to call the function, otherwise it will not run!


I’m starting in Python equally to you and I point out that this is a fairly simple and perhaps even little recommended way to solve this, but without a doubt is a good way to start the user data entry negotiations on your system.

EDIT 1 The function to stop the execution of your code is: Exit()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.