You are on page 1of 2

# Progrma to create, display, search, add, delete and update records in the

Dictionary.
d = {}
while True:
print("1: To create dictionary:")
print("2: To display the dictionary:")
print("3: To search elements in the dictionary:")
print("4: To add elements in the dictionary:")
print("5: To delete elements from the dictionary:")
print("6: To update elements in the dictionary:")
print("Press any other key to exit the program:")
a = input('Enter your CHOICE:')
if a == "1":
n = int(input("How many elements do your want to store in the
dictionary:"))
for i in range(n):
key = input("Enter roll number as a KEY: ")
if key.isdigit():
key = int(key)
no_of_values = int(input("Enter the number of values you want to
store:"))
list_of_values = []
for n in range(no_of_values):
value = input(f"Enter value {n + 1}:")
if value.isdigit():
value = int(value)
list_of_values.append(value)
d[key] = list_of_values
print("Dictionary Created:>",d,"\n")

elif a == "2":
if d == {}:
print("Dictionary is Empty.\n")
else:
for k in d:
print("Roll No:", k)
print("Name:", d[k][0])
print("Marks:", d[k][1])
print()
print()
elif a == "3":
if d == {}:
print("Dictionary is Empty.\n")
else:
roll = int(input("Enter roll number to search:"))
if roll not in d:
print("Roll no. does not exist.")
else:
print("Name:",d[roll][0])
print("Marks:",d[roll][1])
print()
elif a == "4":
while True:
Key = int(input("Enter Roll No:"))
if Key in d:
print("This roll number already exist.")
continue
else:
name = input("Enter name:")
marks = int(input("Enter marks:"))
d[Key] = [name, marks]
print("Data added in the Dictionary.")
ch = input("Do you want to add more record? Y/N:")
if ch in "Yy":
continue
else:
print()
break
elif a == "5":
if d == {}:
print("Dictionary is Empty.\n")
else:
rollno = int(input("Enter roll number to delete:"))
if rollno in d:
v = d.pop(rollno)
print("The elements deleted are:")
print("Name:", v[0])
print("Marks:", v[1])
print()
else:
print("Roll number does not exist.\n")
elif a == "6":
if d == {}:
print("Dictionary is Empty.\n")
else:
rollno = int(input("Enter roll no. to update:"))
if rollno in d:
print("Name:", d[rollno][0])
print("Marks:", d[rollno][1])
ch = input("What do you want to update? N/M:")
if ch in "Nn":
NewName = input("Enter new name:")
d[rollno][0] = NewName
print("Name updated.\n")
elif ch in "Mm":
NewMarks = int(input("Enter marks:"))
d[rollno][1] = NewMarks
print("Marks updated.\n")
else:
print("Wrong choice.\n")
else:
print("Roll number does not exist.\n")
else:
exit()

You might also like