Reverse Output to Python Stack

Asked 1 years ago, Updated 1 years ago, 277 views

We're using Python stack to produce reverse output qwert -> trewq

b=input("Please enter:")

text=list(b)

a=[]

def push():

a.append(text[-1])

def pop():

text.remove(text[-1])

for i in text:

    if len(i) != 0:

      push()

      pop()


else:

print(a)

I did this, but the repetition door stopped in the middle, so what should I do?

python stack

2022-10-12 01:00

1 Answers

Check where and what kind of problems are occurring by printing them out one by one.

You don't know what you want to do or what's wrong with just the code you uploaded.

If you simply want to print it in reverse order, you can do this.

a = [1, 2, 3, 4, 5]
for i in a[::-1]:
    print(i)

Maybe you'll find out what the problem was if you check this out.

a = [1, 2, 3, 4, 5]
for i in a:
    print(i)
    if i == 2:
       a.remove(i)



2022-10-12 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.