Error in Python: NameError name XXX is not defined

1

I am new to Python and I am doing some test exercises, and I am having problems in one that I have invented.

I want to collect names, put them in a vector, and sort them. This had been done before with numbers, but without a while (asking in advance how many numbers were going to be ordered to be able to use a for ).

I already tell you, it will be a bullshit, but I can not see the error:

nombres = []

print("Introduce nombres y los devolvere ordenados. Escribe stop cuando quieras acabar")

stop = "stop"
while (input() != stop):

    nombres.append(input())

nombres.sort()

print(nombres)

I get this error back:

    
asked by parktem 05.07.2018 в 21:15
source

2 answers

2

input() in python3x is the appropriate way to request a data from the user , however in python2x besides asking for a user input, the interpreter evaluates it as if it were Python code, hence the error, the string jaime is being evaluated and the interpreter is just telling you that such a name does not it is defined. In python2x you should use raw_input() or move to version 3x .

On the other hand you have a concept problem, you are asking for data entry but you do not always manage to add it to the list, the input() of while is the one that would be wrong. You could do the following:

nombres = []

print("Introduce nombres y los devolvere ordenados. Escribe stop cuando quieras acabar")

stop = "stop"
valor = raw_input()
while (valor != stop):
    nombres.append(valor)
    valor = raw_input()

nombres.sort()
print(nombres)
    
answered by 05.07.2018 / 21:45
source
1

The biggest problem is that your loop for verifies input twice per loop. This is not to verify the same entry twice, but to ask for two entries per cycle.

nombres = []
stop = "stop"

# Valor inicial
current = input("Introduce nombres y los devolvere ordenados. Escribe stop cuando quieras acabar: ")

while (current != stop):
    nombres.append(current)
    current = input()

nombres.sort()
print(nombres)
    
answered by 05.07.2018 в 21:54