While loop functioning with user input and commands

python while loop user input
how to end a loop with user input python
while loop python
asking the user for input until they give a valid response python
while loop string python
python while loop multiple conditions
how to take continuous input in python
multiple while loops python

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)

While loop functioning with user input and commands, 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,  python - While loop functioning with user input and commands - Stack Overflow 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.

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

How to Indefinitely Request User Input Until Valid in Python, How to Indefinitely Request User Input Until Valid in Python. A step-by-step guide to infinite loops and try/except statements by the function. while loop: I recently wrote about the difference between while and for loops. Inside of the while loop, the program runs a print statement that prompts for the password. Then the variable password is set to the user’s input with the input () function. The program will check to see if the variable password is assigned to the string password, and if it is, the while loop will end.

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)

6.3. Interactive while Loops, As a practical matter, we can greatly improve the utility input functions we have C# loops have a sequence of statements with a definite beginning and end. We need to get input from the user before first beginning the while loop, so we  The body of the do while loop falls between opening and closing braces and contains statements that are to be run in the loop. The while keyword and test expression come after the body of the loop and are terminated by a semicolon (;). do while Loop Example This example demonstrates the do while loop.

While Loops in Python 3, This means that if the user inputs the string password , then the loop will stop Then the variable password is set to the user's input with the input() function. a command-line guessing game that uses a while loop effectively. Sometimes the condition that causes you to terminate a while loop doesn’t occur until somewhere in the middle of the loop. This is especially true when testing user input for some termination character. C++ provides these two control commands to handle this case: break exits the inner most loop immediately. continue passes control back to …

Python Refactoring: "while True" Infinite Loops & The "input" Function, function. use the While loop when you need to validate user input. use loops to You just printed Python is fun! one thousand times, using the range() function, The last two commands you entered are extremely useful, because they allow​  While loop, input from find command Hello nix Experts, I am a *nix rookie and have run into this issue, can some one help me here and let me know what I am doing wrong. Code :

More About Looping, Chapter on loops with simple and practical examples using while loops in Python​. which makes it possible to carry out a sequence of statements repeatedly. Though it's possible to read input like this, usually the function input() is used. The user will then be able to submit a response that will be returned by the function. while loop: I recently wrote about the difference between while and for loops .

Comments
  • You can just do options = 'anything else than exit' right before the loop. There's no need to ask the user before the loop.
  • 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.
  • Works perfectly ! Thanks for you answer !