Restart application in Python

Asked

Viewed 11,849 times

1

How to restart my program with Python?

cpf = input('Digite os nove primeiros dígitos do CPF: ')

if len(cpf) != 9:
    # Aqui deve reniciar a aplicação.
  • 1

    What you call rebooting?

3 answers

3


If you wish to ask for the dice again it would be so:

while True: #inicia o laço
    cpf = input('Digite os nove primeiros dígitos do CPF: ')
    if len(cpf) == 9: #se está ok não precisa mais continuar o laço
        break
    #possivelmente mais código aqui

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Essentially in console applications this is how you do it. Of course you can abstract it so it doesn’t get repetitive. Have other techniques for inputs together.

  • 1

    that I know directly no. There must be some library that allows this. As I would do in . NET?

  • This second question - "has some way of filtering only digits" - is VERY different from the first one - better open another question. But - not directly, you have to program or use a User Interface library - either graphic or text, for that.

2

To restart your application you can create the following method:

import sys
import os
def restart_program():
    python = sys.executable
    os.execl(python, python, * sys.argv)

then when you want to restart the application you call:

restart_program()

More depending on the problem you can make a simple loop repeat for example:

while True:
    cpf = input('Digite os nove primeiros dígitos do CPF: ')
    if len(cpf) == 9:
        break
    else:
        continue

I believe that in your case working with the loop would be the best option

-1

Actually, the comment above is correct, but if it is something only to call again the option to type the CPF, you can use the repetition (As in the comment above):

cpf = input('Digite os nove primeiros dígitos do CPF: ')
if len(cpf) != 9:
     break
     #Para a operação
else:
     continue
  • If this has already been said in the other answer, why did you reply too? I could not understand what you added to the answer that was not in the other answer.

Browser other questions tagged

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