Stop the loop or not

Asked

Viewed 56 times

-3

Good morning,

I’m having difficulty putting a condition in this loop: if tighten 1 loop for loop if tighten 2 out of loop

resp=1

while resp==1:
    cliente_nome.append((input("Digite o nome do Cliente: ")))
    cliente_cpf.append(input("Digite o Cpf: "))
    cliente_idade.append(input("digite a idade: "))
    cliente_depositar.append(input("Quanto desejar depositar: "))
    resp=input("deseja continuar 1-para sim  2-para não")
    if resp==2:
        break
    else:
        continue

2 answers

2


The type of Resp is string and you test if it equals a int. It’ll never work that way.

Two different ways to solve the problem:


First solution

In row 8, change the attribute of Resp for int

resp=int(input("deseja continuar 1-para sim  2-para não"))

Second solution

Change the type of Resp for string in all its references:

resp="1"

while resp=="1":
    cliente_nome.append((input("Digite o nome do Cliente: ")))
    cliente_cpf.append(input("Digite o Cpf: "))
    cliente_idade.append(input("digite a idade: "))
    cliente_depositar.append(input("Quanto desejar depositar: "))
    resp=input("deseja continuar 1-para sim  2-para não")
    if resp=="2":
        break
    else:
        continue

In this particular case, the test at the end is unnecessary because the while is already testing whether Resp == 1 each cycle. Unless you wanted to force the person to write 1 or 2. Because with the code as it is, for any value of Resp other than 1, the program will end. Try typing 3 or 4.

-2

Just put while resp != 2 and so spare him the if else. When you read the value of the console as an integer try to put it like this resp = int(input("..."))

Browser other questions tagged

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