简体   繁体   中英

While loop functioning with user input and commands

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:

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)

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM