Day1 - 26122024
Day1 - 26122024
1. Students = []
2.
3. def addrecord(value):
4. Students.append(value)
5. return
6.
7. def viewlastrecord():
8. if len(Students) == 0:
9. print("Stack underflow!")
10. else:
11. print("Last inserted record:",Students[-1])
12. return
13.
14. def deleterecord():
15. if len(Students) == 0:
16. print("Stack underflow!")
17. else:
18. print("Deleting record:",Students.pop())
19. return
20.
21. def printrecords():
22. for element in Students:
23. print(element)
24.
25. while True:
26. print("""
27. 1. ADD RECORD
28. 2. VIEW LAST INSERTED RECORD
29. 3. DELETE RECORD
30. 4. DISPLAY ALL RECORDS
31. 5. EXIT
32. """)
33. ch = int(input("Enter your choice: "))
34. if ch == 1:
35. stuid = int(input("Enter Student ID: "))
36. stuname = input("Enter student name: ")
37. cls = int(input("Enter Student Class: "))
38. sec = input("Enter Student Section: ")
39. str = input("Enter the stream: ")
40. marks = int(input("Enter the marks: "))
41. s = [stuid, stuname, cls, sec, str, marks]
42. addrecord(s)
43.
44. elif ch == 2:
45. viewlastrecord()
46.
47. elif ch == 3:
48. deleterecord()
49.
50. elif ch == 4:
51. printrecords()
52.
53. elif ch == 5:
54. break
55.
56. else:
Online Class – Day 1
Practice and Doubt Clearing Session
Subject: Computer Science (083)
Date: 26-12-2024
57. print("Invalid option given!")
58.
Online Class – Day 1
Practice and Doubt Clearing Session
Subject: Computer Science (083)
Date: 26-12-2024
Question 2
Write a menu driven program which shows all operations on binary file:
1. Add Record
2. Display all Records
3. Display Specific Record
4. Modify Record
5. Delete Record
Use "data.dat" file which stores the record of "Hotel" in the form of list containing Room_No, Price, Room_type.
1. import pickle
2.
3. def addrecord():
4. f = open("data.dat", "ab")
5. try:
6. record = []
7. roomno = int(input("Enter Room No.: "))
8. price = int(input("Enter price: "))
9. rtype = input("Enter room type [AC/NONAC]: ")
10. record = [roomno, price, rtype]
11. pickle.dump(record,f)
12. print("Record added successfully!")
13. except:
14. print("Error encountered! Unable to add record!")
15. finally:
16. f.close()
17. return
18.
19. def display():
20. f = open("data.dat", "rb")
21. try:
22. while True:
23. record = pickle.load(f)
24. print("Record:",record)
25. except EOFError:
26. print("Displayed all the records!")
27. finally:
28. f.close()
29. return
30.
31. def display_specific():
32. f = open("data.dat", "rb")
33. try:
34. roomno = int(input("Enter room number to display details: "))
35. while True:
36. record = pickle.load(f)
37. if record[0] == roomno:
38. print("Details of the room:",record,sep="\n")
39. break
40. except EOFError:
41. print("Reached end of searching!")
42. finally:
43. f.close()
44. return
45.
46. def modify():
47. f = open("data.dat", "rb")
48. records = []
49. try:
50. while True:
51. record = pickle.load(f)
52. records.append(record)
53. except EOFError:
54. pass
55. finally:
56. f.close()
Online Class – Day 1
Practice and Doubt Clearing Session
Subject: Computer Science (083)
Date: 26-12-2024
57. return
58.
59. roomno = int(input("Enter room number to modify details: "))
60. for record in records:
61. if record[0] == roomno:
62. price = int(input("Enter new price: "))
63. rtype = input("Enter new room type [AC/NONAC]: ")
64. record[1] = price
65. record[2] = rtype
66.
67. f = open("data.dat", "wb")
68. try:
69. for record in records:
70. pickle.dump(record,f)
71. except:
72. print("Error encountered while modification!")
73. finally:
74. f.close()
75. return
76.
77. def delete():
78. # function details
79.
80. # Make the menu
81.