3

I need to make a program to store contacts (Name and phone number). The first step is to make the program run unless the input is 'exit'. The program should offer a set of options. My problem is that when I enter an option it offers again the set of options and I have to enter the option for a second time for it to run.

I kind of understand why the program does that so I tried it with a while True but it didn't work.

def main():
    options = input( "Select an option [add, query, list, exit]:" ) 
    while options != "exit" : 
        options = input( "Select an option [add, query, list, exit]:" )
        # Offrir un choix de commandes
        if options == "add":
            add_contact(name_to_phone)
        if options == "query":
            query_contact(name_to_phone)
        if options == "list":
            list_contacts(name_to_phone)

Select an option [add, query, list, exit]:add
Select an option [add, query, list, exit]:add
Enter the name of a new contact:
2
  • 3
    You can just do options = 'anything else than exit' right before the loop. There's no need to ask the user before the loop. Commented Oct 15, 2019 at 15:42
  • 3
    Just move the first line from your while block to the end of the block. Notice you call it once, enter the while and then call it again. Commented Oct 15, 2019 at 15:43

3 Answers 3

2

It's due to your first option, do like this instead :

def main():
    options = None
    while options != "exit" : 
        options = input( "Select an option [add, query, list, exit]:" )
        # Offrir un choix de commandes
        if options == "add":
            add_contact(name_to_phone)
        if options == "query":
            query_contact(name_to_phone)
        if options == "list":
            list_contacts(name_to_phone)

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly ! Thanks for you answer !
1

You don't need to set any value for 'option' before entering the loop. You can use an infinite loop (while True) to check the value of 'option' inside the loop and take an action accordingly. You can break out of the loop if the user enters "exit". Try this:

def main():
    #options = input( "Select an option [add, query, list, exit]:" ) 
    while True : 
        options = input( "Select an option [add, query, list, exit]:" )
        # Offrir un choix de commandes
        if options == "add":
            add_contact(name_to_phone)
        if options == "query":
            query_contact(name_to_phone)
        if options == "list":
            list_contacts(name_to_phone)
        if options == "exit":
            break

Comments

0

That is because your first line in while loop is also asking for options.

You can remove the line options = input( "Select an option [add, query, list, exit]:" before the while loop and set an option = '' at starting.

def main():
options = '' 
while options != "exit" : 
    options = input( "Select an option [add, query, list, exit]:" )
    # Offrir un choix de commandes
    if options == "add":
        add_contact(name_to_phone)
    if options == "query":
        query_contact(name_to_phone)
    if options == "list":
        list_contacts(name_to_phone)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.