I am creating a basic phonebook program that can perform operations (add new, update, delete, lookup and exit). I have the first option as add entry and the second as delete entry. Every time the user completes an action, the option to select an action again should come up. When the user selects for the second time, it brings back to the first item instead of the selection; For example; 1 is to add new contact, 2 is to delete new contact. User selects 1, adds new contact is asked to select another option, chooses 2 but the code for option 1 runs again to add new instead of delete.
print("Please select an option from the main menu :\n")
def print_menu():
print("1 : Add New Entry")
print("2 : Delete Entry")
print("3 : Update Entry")
print("4 : Lookup Number")
print("5 : QUIT")
print()
print_menu()
while 1 == 1:
try:
menuOption = int(input())
break
except:
print("Invalid! Please choose an option between 1 and 5.")
while menuOption not in(1,2,3,4,5):
print("Invalid! Please choose an option between 1 and 5.")
try:
menuOption = int(input())
break
except:
print("Invalid! Please choose an option between 1 and 5.")
###the above works perfect to set menu and restrict entry
phonebook = {}
#while menuOption != 5:
#menuOption = int(input("Enter your selection (1-5): "))
while 1 == 1 :
if menuOption == 1: #
print("\nNEW CONTACT")
while True:
name = input("Contact Name : ")
if name.replace(' ','').isalpha():
break
print('Please enter a valid name.')
while True:
try:
number = int(input("Contact Number : "))
if number:
break
except:
print("Please enter a valid number.")
if number in phonebook:
print("Contact already exists. Duplicates are not allowed.\n")
else:
phonebook[number] = name
print("Success! New contact has been added.\n")
print("PLEASE SELECT AN OPTION BETWEEN 1 AND 5 \n")
try:
option = int(input())
except:
print("Please enter a numeric value between 1 and 5 \n")
elif menuOption == 2: ##delete
print("\nDELETE CONTACT")
name = input("Contact Name : ")
if name in phonebook:
del phonebook[name]
print(name, "has been removed from your contacts.")
else:
print("Contact not found.")
print("PLEASE SELECT AN OPTION BETWEEN 1 AND 5 \n")
try:
option = int(input())
except:
print("Please enter a numeric value between 1 and 5 \n")
Welcome to the stack, rainyday! On looking at/running your code, the message to remind the user to enter between one and five came up a bit more than I expected, along with random other errors that you probably just haven't coded for yet. I would suggest that defining more functions (for the menu options) and structuring your code a bit more would make your code easier to read and follow.
Below (which is not complete or error-free btw), I re-structured your code so that when main()
is called, the Phone Book menu options shows and the user can choose another option. Instead of using long "else-if"/elif s between the various functions, the various menu routines are neatly organised in one while
statement in the main()
function and the choices are organised into 5 different functions: add()
/delete()
etc. (I put dummy code into the update/lookup/exit fns). I hope you find this helpful. I did find that if I entered a string when a digit was expected as input that it threw an error message. I inserted comments to assist.
Hope this helps
phonebook= []
def main():
print("\n\tPhone Book") #title
# main menu
print("\n\tMain Menu")
print("\t1. Add a contact")
print("\t2. Delete a contact")
print("\t3. Update a contact")
print("\t4. Look up")
print("\t5. Quit")
menuOption = int(input("\nPlease select one of the five options "))
while menuOption not in(1,2,3,4,5) :
## insert try/except error handling needed here to handle NaN ##
print("\nPlease insert a numeric option between 1 and 5")
menuOption =int(input())
while menuOption <=5:
mOpt =menuOption
if menuOption == 1:
Add(mOpt)
elif menuOption == 2:
Delete(mOpt)
elif menuOption == 3:
Update(mOpt)
elif menuOption == 4:
LookUp(mOpt)
elif menuOption == 5:
ExitPhone(mOpt)
else:
print("Invalid input! Please insert a value between 1 and 5")
# add contact
def Add(mOpt):
##Option 1
add = ""
contact = True
print("\n\tADD CONTACT")
while contact == True:
if mOpt == 1:
print("\nNEW CONTACT")
while True:
name = input("Contact Name : ")
if name.replace(' ','').isalpha():
break
print('Please enter a valid name.')
while True:
try:
number = int(input("Contact Number : "))
if number:
break
except:
print("Please enter a valid number.")
if number in phonebook:
print("Contact already exists. Duplicates are not allowed.\n")
else:
#item = name + number this won't be found in the delete function
phonebook.append(name)
phonebook.append(number)
#print(phonebook)
print("Success! New contact has been added.\n")
add = input("Would you like to add another? Y (yes) or N (no)")
add = add.lower()
if add=="yes" or add=="y":
contact = True
else:
contact = False
main()
# delete
def Delete(mOpt):
redel = ""
delcontact = True
print("\n\tDELETE CONTACT")
while delcontact == True:
if mOpt == 2:
print("Enter Contact Name:")
name = input("Contact Name : ")
if name in phonebook:
## insert code here to find associated number
## and concatenate it if you have created an 'item'
phonebook.remove(name) #removes name, leaves the number
print(name, "has been removed from your contacts.")
#print(phonebook)
else:
print("Contact not found.")
redel = input("Would you like to delete another? Y (yes) or N (no)")
redel = redel.lower()
if redel =="yes" or redel =="y":
delcontact = False
else:
delcontact = True
main()
def Update(mOpt):
if mOpt == 3:
print("\nUpdate function")
main()
def LookUp(mOpt):
if mOpt == 4:
print("\nLookUp function")
main()
def ExitPhone(mOpt):
if mOpt == 5:
print ("Exit?")
main()
main()