Problem Statement: Create a dictionary containing names and
marks as key-value pairs of 6 students. Write a MENU BASED Python
program, with separate user-defined functions, to perform the
following:
1. Push the keys (names of the students) into a stack, where the
corresponding value (marks) is greater than 75.
2. Pop and display the contents of the stack.
Example:
If the dictionary is:
R = {"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
Then the program output should be:
TOM ANU BOB OM
Code:
R = {}
stack = []
MAX = 5
def push():
if len(stack) >= MAX:
print("Stack Overflow")
else:
name = input("Enter student name: ")
marks = int(input("Enter marks: "))
R[name] = marks
if marks > 75:
[Link](name)
print("Pushed into stack")
else:
print("Marks not greater than 75, not pushed")
def pop():
if len(stack) == 0:
print("Stack Underflow")
else:
print("Popped element:", [Link]())
def display():
if len(stack) == 0:
print("Stack Empty")
else:
print("Stack contents:")
print(stack[::-1])
def menu():
while True:
print("\n1. Push Element")
print("2. Pop Element")
print("3. Display Stack")
print("4. Exit")
ch = int(input("Enter your choice: "))
if ch == 1:
push()
elif ch == 2:
pop()
elif ch == 3:
display()
elif ch == 4:
break
else:
print("Invalid choice!")
menu()
Output: