If you can use something ready have at least two techniques:
lista = []
while True:
i = int(input('Digite um número: '))
if i < 1: break
lista.append(i)
for i in lista[::-1]:
print(i)
for i in reversed(lista):
print(i)
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Both generate an iterator for the list in the inverted order. The first uses the Slice to indicate that it starts from beginning to end as no starting and ending position has been placed before the two signs of :
and in the end it puts of how much on how much it should walk, therefore it shows that it should go in negative position, so it indicates that it should go decreasing each position.
Never change the composition of a complex object as a list during the loop, mainly by deleting items, this will almost always go wrong because the iterator was prepared for something that no longer exists. You can change the internal data but not the list as it was done.
In addition the problem asks to print reversed and not keep erasing the data.
I modified the loop because I think it’s wrong to ask for the die in and out of the loop (see DRY). In a simple example may not cause problems, but get used wrong. There I solved some other problems that happened in the code, but it is not the focus of the question.
I did not consider that typing can be wrong and break the application, the correct is to treat it by capturing exception, or creating a proper function that better handles it.
If the problem prevented using any of the language’s own resources then it would have to iterate each item starting from the last one to the first one, having a counter to control it, but in real codes this restriction does not make sense.
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero