0% found this document useful (0 votes)
5 views2 pages

Python 26 Stack

This document contains a Python script that implements a simple stack data structure with a menu for adding, deleting, and displaying records. It allows a maximum of five records and includes functions for managing the stack and saving data to a file. The script runs in an infinite loop until the user chooses to quit.

Uploaded by

hemaxiao7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Python 26 Stack

This document contains a Python script that implements a simple stack data structure with a menu for adding, deleting, and displaying records. It allows a maximum of five records and includes functions for managing the stack and saving data to a file. The script runs in an infinite loop until the user chooses to quit.

Uploaded by

hemaxiao7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#4B 27 Yim Chun Yu

#Python26 Stack
max = 5
stack = []
x=0
def Menu():
global choice
global x
print("====Menu====")
print("1.Add a record")
print("2.Delete a record")
print("3.Display List")
print("4.Quit")
choice = int(input("Enter your choice:"))
def Addrecord():
global x
global stack
global max
if len(stack) < max:
name = str(input(("Enter a name:")))
stack[:0] = [name]
print(name, "is added.")
else:
print("The stack is full. You cannot add any record")
def Deleterecord():
global x
global stack
global max
if len(stack) > 0:
f = open("DataFile.txt", "w")
stack.pop(len(stack)-1)
print("A record is deleted")
else:
print("The stack is empty. You cannot delete any record.")
def Displaylist():
print(*stack, sep="\n")
def OpenFile():
f = open("DataFile.txt", "w")
def SaveFile():
f = open("DataFile.txt", "r")
f.close()
OpenFile()
while 1 == 1:
Menu()
if choice == 1:
Addrecord()
elif choice == 2:
Deleterecord()
elif choice == 3:
Displaylist()
elif choice == 4:
SaveFile()

You might also like