End loop when an entry is empty

0

I have the following situation: A repeat loop that asks for a name, and 2 notes, where the data entry should finish when you read an empty name, but the way below the second time the program runs the name is not asked, any tips on how to do?

nome = input("Nome: ")

while nome != "":
    n1 = int(input("N1: "))
    n2 = int(input("N2: "))
    
asked by anonymous 24.09.2017 / 17:58

2 answers

1

In order for the loop to end with% void%, it is necessary that nome be read back into nome , like this:

nome = input("Nome: ")

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

Note that it could not just be like this:

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: ")
    
24.09.2017 / 18:26
0

I tried reading in the While, but it seems to me that in Python this does not 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 looping.

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: "))
    
24.09.2017 / 20:50