0

names = set()
emails = set()

print('-=-=- Menu -=-=-\n')

v = print('V. View Contacts')
a = print('A. Add New Contact')
c = print('C. Change Contact')
d = print('D. Delete Contact')
q = print('Q. Quit\n')

choice = str(input('Enter your choice: '))

if choice =='A' or 'a':
    print('-=-=- Enter the New Contact Name and Email -=-=-\n')
    
    add_name = str(input('Enter Contact Name: '))
    add_email = str(input('Enter Contact Email: '))
    names.add(add_name)
    emails.add(add_email)

    choice = str(input('Enter your choice: '))
    
elif choice == 'V' or 'v':
    print('-=-=-=-=-=-=-=-=-=-=-=-\n')
    print('-=-=-=- Name, Email -=-=-=-')
    print(names, emails, '\n')
    
    choice = str(input('Enter your choice: '))
    
elif choice == 'C' or 'c':
    print('-=-=-=-=-=-=-=-=-=-=-=-\n')
    changed_name = str(input('Enter Contact Name:'))
    changed_email = str(input('Enter Contact Email: '))

    names.update(changed_name)
    emails.update(changed_email)
    
elif choice == 'D' or 'd':
    print('-=-=-=-=-=-=-=-=-=-=-=-\n')
    delete_name = str(input('Enter Contact Name You Wish to Delete: '))
    delete_email = str(input('Enter the Email of the Contact Name: '))
    
    names.discard(delete_name)
    emails.discard(delete_email)
        
elif choice =='Q' or 'q':
    print('Goodbye')
    exit()    

Even though I'm selecting a different option(like view contacts or change contact), it automatically tries to add a new contact. If you could break down what my mistakes are and how I am able to fix them, it would be very much appreciated.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
BeanBagels
  • 33
  • 3
  • 3
    `or 'a'` is always true – alec Nov 15 '21 at 06:08
  • from what I see, here are some offers for your code 1. no need to cast `str(input())` since `input` returns a `str` type. 2. sounds like you are aiming for a few operations in a run. If so - I recommend using a loop that will contain the whole `if` statement. then, you can do`choice = input('Enter your choice: ')` in the start of the loop, instead inside each if-else option 3. your if statements should have the form of `choice in ('V', 'v')` instead of `choice == 'V' or 'v'` – jsofri Nov 15 '21 at 06:15
  • Next time please restrict your code to a [mre]. You could have removed 80% of your lines, replace the content of each conditional clause by a print statement. Nothing in your question regarding your problem is doing anything with your task-statement. You never need to store the output of a print statement - the result of print is always `None` so thats not needed as well etc. – Patrick Artner Nov 15 '21 at 06:15

1 Answers1

0

Your conditionals are wrong.

if choice == 'A' or 'a':

is evaluated as if (choice == 'A') or ('a') and 'a' is always truthy. That's why none of the other elif branches get chosen.

Either

if choice == 'A' or choice == 'a':

or the more Pythonic

if choice in ('a', 'A'):

or maybe even

if choice.lower() == 'a':
AKX
  • 152,115
  • 15
  • 115
  • 172