End loop when an entry is empty

Asked

Viewed 880 times

0

I have the following situation: A repeat loop that asks for name, and 2 notes, where the data input should end when an empty name is read, but in the way below the second time the program runs the name is not asked, some hint of how to do ?

nome = input("Nome: ")

while nome != "":
    n1 = int(input("N1: "))
    n2 = int(input("N2: "))
  • 1

    The nome is not being read inside the while soon will never end as soon as it will never be asked again

  • Leaving the "name" inside the while I have the following problem: Nameerror: name 'name' is not defined

2 answers

1

So that the bond can end with the nome emptiness it is necessary that the nome be read again inside the while, thus:

nome = input("Nome: ")

while nome != "":
    n1 = int(input("N1: "))
    n2 = int(input("N2: "))
    nome = input("Nome: ") #leitura novamente aqui

Note that it couldn’t just stay that way:

while nome != "": #dá erro nesta linha porque o nome ainda não existe aqui, só dentro do while
    n1 = int(input("N1: "))
    n2 = int(input("N2: "))
    nome = input("Nome: ")

0

I tried to read it right away on While, but it seems to me that in Python it doesn’t work like in Java.

So, a solution, I created a function that reads a name and returns the name, while that name is different from empty, it will continue to loop.

nome=""
def ler():      #função para ler
    global nome
    nome = input("Digite o nome:")
    return nome #retorno da função
while ler()!="":         #comparação.
    n1 = int(input("N1: "))
    n2 = int(input("N2: "))

Browser other questions tagged

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