Does not satisfy the condition with the value informed by the user

Asked

Viewed 33 times

-1

I made this code, but it does not enter the option informed by the user (I think the option already tells what he has to do).

lista = []
while True:
    print("Adicionar nome - 1")
    print("Sair - 0")
    print("Listar - 2")
    op = input()

    if op == 0:
        break
    elif op == 1:
        nome = input("Digite o nome")
        lista.append(nome)
    elif op == 2:
        j = 0
        for nomes in lista:
            print(nomes)
  • Set the input type for the input. This will solve your problem: op = int(input())

1 answer

4


The entrance input() makes the user type a string, for this reason, when you compare op == 0 you are comparing a string with an integer. To solve this you have 2 options:

1st:

op = input()
if op == '0':
   break

2nd: Use int(input()) to receive a whole

op = int(input())
if op == 0:
   break

Browser other questions tagged

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