Menu Driven Programs Bluestocks
Menu Driven Programs Bluestocks
1. Dictionary
'''
5. Write a program to input your friends’ names and their Phone Numbers and store them in
the dictionary as the key-value
pair. Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names
g) Exit
'''
dic = {}
#Creates an empty dictionary
#While loop to provide the options repeatedly
#it will exit when the user enters 7
while True:
print("1. Add New Contact")
print("2. Modify Phone Number of Contact")
print("3. Delete a Friend's contact")
print("4. Display all entries")
print("5. Check if a friend is present or not")
print("6. Display in sorted order of names")
print("7. Exit")
inp = int(input("Enter your choice(1-7): "))
#Adding a contact
if(inp == 1):
name = input("Enter your friend name: ")
phonenumber = input("Enter your friend's contact number: ")
dic[name] = phonenumber
print("Contact Added \n\n")
#Modifying a contact if the entered name is present in the dictionary
elif(inp == 2):
name = input("Enter the name of friend whose number is to be modified: ")
if(name in dic):
phonenumber = input("Enter the new contact number: ")
dic[name] = phonenumber
print("Contact Modified\n\n")
else:
print("This friend's name is not present in the contact list")
#Deleting a contact if the entered name is present in the dictionary
elif(inp == 3):
name = input("Enter the name of friend whose contact is to be deleted: ")
if(name in dic):
del dic[name]
print("Contact Deleted\n\n")
else:
print("This friend's name is not present in the contact list")
#Displaying all entries in the dictionary
elif(inp == 4):
print("All entries in the contact")
for a in dic:
print(a,"\t\t",dic[a])
print("\n\n\n")
#Searching a friend name in the dictionary
elif(inp == 5):
name = input("Enter the name of friend to search: ")
if(name in dic):
print("The friend",name,"is present in the list\n\n")
else:
print("The friend",name,"is not present in the list\n\n")
#Displaying the dictionary in the sorted order of the names
elif(inp == 6):
print("Name\t\t\tContact Number")
for i in sorted([Link]()):
print(i,"\t\t\t",dic[i])
print("\n\n")
#Exit the while loop if user enters 7
elif(inp == 7):
break
#Displaying the invalid choice when any other values are entered
else:
print("Invalid Choice. Please try again\n")
2. Strings
'''
WAP to input a sentence and perform the following operations:
1. Count number of characters.
2. Count number of capital letters
3. Count number of small letters
4. Count number of blank spaces
5. Count number of digits
6. Count number of special or other characters
7. Number of alphabets
8. Number alphanumeric characters
9. Number of words
10. Exit
'''
s=input("Enter a sentence: ")
choice=0
while True:
print("********MAIN MENU*********")
print("--------------------------")
print("1. Count number of characters")
print("2. Count number of capital letters")
print("3. Count number of small letters")
print("4. Count of blank spaces")
print("5. Count number of digits")
print("6. Count number of special or other characters")
print("7. Count number of Number of alphabets")
print("8. Count number of Number of alphanumeric characters")
print("9. Count number of words")
print("10. Exit")
print("--------------------------")
choice=int(input("Enter your chaoice (1..10): "))
if choice==1:
print("Given string: ", s)
L=len(s)
print("Number of characters=",L)
if choice==2:
print("Given string: ", s)
caps=0
for i in range(len(s)):
if s[i].isupper():
caps+=1
print("Number of capital letters=",caps)
if choice==3:
print("Given string: ", s)
small=0
for i in range(len(s)):
if s[i].islower():
small+=1
print("Number of small letters=",small)
if choice==4:
print("Given string: ", s)
space=0
for i in range(len(s)):
if s[i].isspace():
space+=1
print("Number of blank spaces=",space)
if choice==5:
print("Given string: ", s)
digit=0
for i in range(len(s)):
if s[i].isdigit():
digit+=1
print("Number of digits=",digit)
if choice==6:
print("Given string: ", s)
Others=0
for i in range(len(s)):
if s[i].isalnum()==False and s[i].isspace()==False:
Others+=1
print("Number of other / special characters=",Others)
if choice==7:
print("Given string: ", s)
Alpha=0
for i in range(len(s)):
if s[i].isalpha():
Alpha+=1
print("Number of alphabets=",Alpha)
if choice==8:
print("Given string: ", s)
alno=0
for i in range(len(s)):
if s[i].isalnum():
alno+=1
print("Number of alphanumric characters=",alno)
if choice==9:
print("Given string: ", s)
words=0
words=len([Link]())
print("Number of blank words=",words)
if choice==10:
print("Ending the program !")
exit()
3. Dictionary
'''
Program 10-5 Write a menu-driven program for the following:
Create a dictionary ‘ODD’ of odd numbers between 1 and 10, where the key is the decimal
number and the value is the corresponding number in words. Perform the following
operations in this dictionary:
1. Create Dictionary ODD and display it
2. Display the keys
3. Display the values
4. Display the items (keys and values)
5. Find the length of the dictionary
6. Check if 7 is present or not
7. Check if 2 is present or not
8. Retrieve the value corresponding to the key 9
9. Delete the item from the dictionary corresponding to the key 9
10. Exit
'''
choice=0
while choice!=10:
print("-------------------------------------------------------------------")
print(" MAIN-MENU")
print("-------------------------------------------------------------------")
print("1. Create dictionary ODD and display it")
print("2. Display the keys")
print("3. Display the values")
print("4. Display the items(keys and values)")
print("5. Find the length of the dictionary")
print("6. Check if 7 is present or not")
print("7. Check if 2 is present or not")
print("8. Retrieve the value corresponding to the key 9")
print("9. Delete the item from the dictionary corresponding to the key 9")
print("10. Exit")
print("-------------------------------------------------------------------")
choice=int(input("Enter your choice: (1..10): "))
if choice==1:
print("Creating Dictionary ODD ")
ODD = {1:'One',3:'Three',5:'Five',7:'Seven',9:'Nine'}
print("Displaying Dictionary ODD ")
print(ODD)
elif choice==2:
print("Displaying keys of Dictionary ODD: ")
print([Link]())
elif choice==3:
print("Displaying values of Dictionary ODD: ")
print([Link]())
elif choice==4:
print("Displaying keys and values of Dictionary ODD: ")
print([Link]())
elif choice==5:
print("Find the length of the dictionary: ")
print(len(ODD))
elif choice==6:
print("Check if 7 is present or not")
print(7 in ODD)
elif choice==7:
print("Check if 2 is present or not")
print(2 in ODD)
elif choice==8:
print("Retrieve the value corresponding to the key 9")
print([Link](9))
elif choice==9:
print("Delete the item from the dictionary corresponding to the key 9")
del ODD[9]
print("Dictionary ODD after deletion:")
print(ODD)
elif choice==10:
exit()
else:
print("Invalid choice!")
4. Lists
'''
Write a menu driven program to perform
various list operations, such as:
• Append an element
• Insert an element
• Append a list to the given list
• Modify an existing element
• Delete an existing element from its position
• Delete an existing element with a given value
• Sort the list in ascending order
• Sort the list in descending order
• Display the list.
• Exit
'''
#Program
#Menu driven program to do various list operations
myList = [] #[22,4,16,38,13] #myList
n=int(input("Enter number of elements in the list: "))
for i in range(n):
num=int(input("Enter a number: "))
[Link](num)
choice = 0
while True:
print("The list 'myList' has the following elements", myList)
print("\n MENU LIST OPERATIONS")
print(" 1. Append an element")
print(" 2. Insert an element at the desired index position")
print(" 3. Append a list to the given list")
print(" 4. Modify an existing element")
print(" 5. Delete an existing element by its position")
print(" 6. Delete an existing element by its value")
print(" 7. Sort the list in ascending order")
print(" 8. Sort the list in descending order")
print(" 9. Display the list")
print(" 10. Exit")
choice = int(input("ENTER YOUR CHOICE (1-10): "))
#append element
if choice == 1:
element = int(input("Enter the element to be appended: "))
[Link](element)
print("The element has been appended\n")
#insert an element at desired position
elif choice == 2:
element = int(input("Enter the element to be inserted: "))
pos = int(input("Enter the index for inserting: "))
[Link](pos,element)
print("The element has been inserted\n")
#append a list to the given list
elif choice == 3:
newList=[]
n=int(input("Enter number of elements in new list: "))
for i in range(n):
num= int(input("Enter a number to be appended: "))
[Link](num)
[Link](newList)
print("The list has been appended\n")
#modify an existing element
elif choice == 4:
i = int(input("Enter the index of the element to be modified: "))
if i < len(myList):
newElement = int(input("Enter the new element: "))
oldElement = myList[i]
myList[i] = newElement
print("The element",oldElement,"has been modified\n")
else:
print("Position of the element is more than the length of list")
#delete an existing element by position
elif choice == 5:
i = int(input("Enter the index of the element to be deleted: "))
if i < len(myList):
element = [Link](i)
print("The element",element,"has been deleted\n")
else:
print("\nIndex of the element is more than the length of list")
#delete an existing element by value
elif choice == 6:
element = int(input("\nEnter the element to be deleted: "))
if element in myList:
[Link](element)
print("\nThe element",element,"has been deleted\n")
else:
print("\nElement",element,"is not present in the list")
#list in sorted order
elif choice == 7:
[Link]()
print("\nThe list has been sorted")
#list in reverse sorted order
elif choice == 8:
[Link](reverse = True)
print("\nThe list has been sorted in reverse order")
#display the list
elif choice == 9:
print("\nThe list is:", myList)
#exit from the menu
elif choice == 10:
break
else:
print("Choice is not valid")
print("\n\nPress any key to continue..............")
ch = input()