Variable check if the field is not typed in Python?

0

I need to check if the variable was filled with a name, I tried to check with If and with break however the field keeps filling without typing a word .... Below is the code I'm doing ...

opcao = 0
while opcao != 6:
    print("""3[32m
Em relação aos contatos do sistema, você deseja...

    1 - Inserir
    2 - Buscar
    3 - Listar
    4 - Alterar
    5 - Excluir
    6 - Voltar
3[0;0m""")

    opcao = int(input("3[32mInforme a opção desejada: 3[0;0m"))

    if opcao == 1:
        print("\n3[47m3[30m--- Digite os dados do contato ---3[0;0m\n")

        n = input("Nome: ")
        t = input("Telefone: ")
        e = input("E-mail: ")
        i = int(input("Id: "))

        if n == "":
            print("\n3[47m3[30mEspaço vazio! Digite um nome...3[0;0m")

        if t == "":
            print("\n3[47m3[30mEspaço vazio! Digite um login...3[0;0m")

        if e == "":
            print("\n3[47m3[30mEspaço vazio! Digite um senha...3[0;0m")

        print("\n3[47m3[30m--- Contato inserido com sucesso ---3[0;0m\n")

        inserir_contato(conexao, n, t, e, i)
    
asked by anonymous 04.10.2018 / 21:00

1 answer

0

You printed the message

print("\n3[47m3[30mEspaço vazio! Digite um nome...3[0;0m")

But the code keeps running and inserts the empty name anyway! You can only enter the name if it is filled in.

One way is to use break as you said, but this keyword only works within a repeat block like for or while :

while True: 
    n = input("Nome: ")
    if n == "":
        print("\n3[47m3[30mEspaço vazio! Digite um nome...3[0;0m")
    else:
        break

In this way the structure will repeat infinitely ( while True ) and will only stop when it reaches break , in this case, if the user does not leave the field blank ( else ).

In other words: if the user leaves the field blank, the error message will be printed and the code will repeat until the user enters a name, causing the code to exit the replay through break .

    
04.10.2018 / 21:48