Q. Write a program that depending upon user's choice, either pushes or pops an element in a stack.


You can understand by Watching video :-



Answer =

stack = [ ]
while True :
    com = input("Enter ( Push or  Pop ) : ")
    
    if com == "push" or com == "Push" :
        num = int(input("Enter a number : "))
        stack.append(num)

    elif  len(stack) == 0 :
        print("Underflow")
        
    elif com == "pop" or com == "Pop" :
        stack.pop()

    yes = input ("Enter ( Yes or no ) : ")

    if yes == "No" or yes == "no":
        print("Thank you !!!!")
        break
    print(stack)

Output :-

Enter ( Push or  Pop ) : push
Enter a number : 5
Enter ( Yes or no ) : yes
Enter ( Push or  Pop ) : push
Enter a number : 6
Enter ( Yes or no ) : yes
Enter ( Push or  Pop ) : push
Enter a number : 3
Enter ( Yes or no ) : yes
Enter ( Push or  Pop ) : pop
Enter ( Yes or no ) : yes
Enter ( Push or  Pop ) : push
Enter a number : 1
Enter ( Yes or no ) : no
Thank you !!!!
[5, 6, 1]

>>>

2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Do program will run without else part????

    ReplyDelete
    Replies
    1. Yes, you can run this program in your pc.

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post