0% found this document useful (0 votes)
13 views7 pages

Menu Driven Programs

Uploaded by

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

Menu Driven Programs

Uploaded by

tanumisha99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MENU DRIVEN PROGRAMS

CSV
'''menu driven csv'''
import csv
def wcsv():
with open('myfile.csv',mode='a') as cf:
mywriter=csv.writer(cf,delimiter=',')
a='y'
while a=='y':
eno=int(input('Enter employee num: '))
name=input('Enter the name: ')
s=int(input('Enter the salary: '))
mywriter.writerow([eno,name,s])
print('#DATA SAVED#')
a=input('Add more?')

def rcsv():
with open('myfile.csv',mode='r') as rf:
mr=csv.reader(rf,delimiter=',')
print('%10s'%'EMPNO','%20s'%'EMP NAME','%10s'%'SALARY')
print('======================================================')
for row in mr:
if row!=[]:
print('%10s'%row[0],'%20s'%row[1],'%10s'%row[2])

def ccsv():
with open('myfile.csv',mode='r') as r:
mr=csv.reader(r,delimiter=',')
c=0
print('%10s'%'EMPNO','%20s'%'EMP NAME','%10s'%'SALARY')
print('======================================================')
for row in mr:
if row!=[]:
print('%10s'%row[0],'%20s'%row[1],'%10s'%row[2])
c+=1
print('======================================================')
print('TOTAL RECORDS: ',c)
print('======================================================')

def scsv():
with open('myfile.csv') as f:
mr=csv.reader(f,delimiter=',')
found=False
e=int(input('Enter employee num to search: '))
for r in mr:
if len(r)!=0:
if int(r[0])==e:
print('===============================')
print('Name: ',r[1])
print('Salary: ',r[2])
found=True
break
if not found:
print('EMPLOYEE NOT FOUND')

def menu():
print('MENU:')
print('1. To write in a CSV file.')
print('2. To read a CSV file.')
print('3. To display the count of CSV file.')
print('4. To search in a CSV file.')

c='y'
while c=='y':
menu()
i=int(input('Enter your choice: '))
if i==1:
wcsv()
c=input('Do you want to continue? (y/n)')
elif i==2:
rcsv()
c=input('Do you want to continue? (y/n)')
elif i==3:
ccsv()
c=input('Do you want to continue? (y/n)')
elif i==4:
scsv()
c=input('Do you want to continue? (y/n)')
else:
print('INVALID CHOICE')
BINARY
'''Write a MENU driven Binary file handling program for student information with roll number, name,
marks.'''
'''append,read,search,update,delete'''
import pickle
import os
def bf_append(filename):
f = open(filename,"ab")
data_log = []
print("Append Data")
rno = int(input("Enter the Roll No:"))
sname = input("Enter Student Name:")
score = int(input("Enter marks:"))
data_log.append([rno,sname,score])
pickle.dump(data_log,f)
f.close()

def bf_read():
f = open("students.dat","rb")
print("*"*78)
print("Data stored in File....")
try:
while True:
data= pickle.load(f)
for record in data:
print("Roll No.",record[0])
print("Name: ",record[1])
print("Marks: ",record[2])
print("."*78)
except Exception:
f.close()

def bf_search():
f = open("students.dat","rb")
pc = int(input("Roll Num of student to search: "))
tf = 0
try:
while True:
data= pickle.load(f)
for record in data:
if record[0]==pc:
print("Name:",record[1])
print("Marks:",record[2])
tf = 1
break
except Exception:
f.close()
if tf==0: #in line with except
print("Record not found...")
else:
print("Record Found....")

def bf_update():
f = open("students.dat","rb")
f1=open("temp.dat","wb")
pc = int(input("Roll Num to update records:"))
f.seek(0)
try:
while True:
current_pos = f.tell()
data= pickle.load(f)
for record in data:
if record[0]==pc:
record[1]=input("Enter Name to update:")
record[2]=input("Enter Marks to update:")
f.seek(current_pos)
pickle.dump(data,f1)
break
except Exception:
f.close()
#os.remove("students.dat")
os.rename("temp.dat","students.dat")

def bf_delete():
f = open("students.dat","rb")
data=pickle.load(f)
f.close()
pc = int(input("Roll Num to delete a record:"))
f = open("students.dat","wb")
lst=[]
for record in data:
if record[0]==pc:
continue
lst.append(record)
pickle.dump(lst,f)
print('record deleted')
f.close()

def menu():
print('MENU:')
print('1. To append data in a file.')
print('2. To Read data in a file.')
print('3. To Search data in a file.')
print('4. To update data in a file')
print('5. To delete data in a file')

c='y'
while c=='y':
menu()
i=int(input('Enter your choice: '))
if i==1:
bf_append('students.dat')
c=input('Do you want to continue? (y/n): ')
elif i==2:
bf_read()
c=input('Do you want to continue? (y/n): ')
elif i==3:
bf_search()
c=input('Do you want to continue? (y/n): ')
elif i==4:
bf_update()
c=input('Do you want to continue? (y/n): ')
elif i==5:
bf_delete()
c=input('Do you want to continue? (y/n): ')
else:
print('Invalid choice!')
c=input('Do you want to continue? (y/n): ')
STACK
def push(stack, doctor):
stack.append(doctor)
print("Pushed: " + str(doctor))

def pop(stack):
if stack:
removed_doctor = stack.pop()
print("Popped: " + str(removed_doctor))
else:
print("Stack is empty, cannot pop.")

def display(stack):
if stack:
print("Current Stack:")
for doctor in reversed(stack): # Display top of stack first
print("ID: " + str(doctor[0]) + ", Name: " + doctor[1] + ", Specialization: " + doctor[2] + ",
Mobile: " + doctor[3])
else:
print("Stack is empty.")

def main():
stack = []

while True:
print("\nMenu:")
print("1. Push Doctor Record")
print("2. Pop Doctor Record")
print("3. Display Stack")
print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':
doc_id = input("Enter Doctor ID: ")
doc_name = input("Enter Doctor Name: ")
specialization = input("Enter Specialization: ")
mobile = input("Enter Mobile Number: ")
doctor = [doc_id, doc_name, specialization, mobile] # Nested list for doctor info
push(stack, doctor)

elif choice == '2':


pop(stack)

elif choice == '3':


display(stack)

elif choice == '4':


print("Exiting.")
break

else:
print("Invalid choice. Please try again.")

# Call the main function


main()

You might also like