-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprg49.py
50 lines (44 loc) · 1.61 KB
/
prg49.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Python program to create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.
import pickle
studList = {}
def writer():
with open("studentList.dat", "ab") as studListBin:
rng = int(input("Enter the number of records you want to add : "))
for i in range(rng):
studList["roll number"] = int(input("Enter the roll number : "))
studList["name"] = input("Enter the name : ")
pickle.dump(studList, studListBin)
def display():
with open("studentList.dat", "rb") as studListBin:
try:
while True:
content = pickle.load(studListBin)
print(content)
except EOFError:
pass
def search():
success = 0
rollNumberToSearch = int(input("Enter the roll number to search : "))
with open("studentList.dat", "rb") as studListBin:
try:
while True:
content = pickle.load(studListBin)
if content["roll number"] == rollNumberToSearch:
print(content)
success = 1
except EOFError:
print("Record not found...")
def main():
print("========== MENU ==========\n",
"1. Insert record\n",
"2. Display\n",
"3. Search\n")
while True:
choice = int(input("Enter the choice : "))
if choice == 1:
writer()
elif choice == 2:
display()
elif choice == 3:
search()
main()