I just do not know how to do this. Any help is appreciated. Create a new variable, total don't call it sumthat's a Python built-ininitialise it to 0 and add number to it with each iteration. You'd do this in much the same way as you're currently calculating numbersEntered. Then you need to correct your average calculation to take total into account.
Learn more. How to add all the user inputs in a while loop? Ask Question. Asked 7 years, 2 months ago. Active 7 years, 2 months ago. Viewed 6k times. Active Oldest Votes. You don't 'need to add all the user inputs together once the user enters "N"', per se.
Johnsyweb Johnsyweb k 20 20 gold badges silver badges bronze badges. B: I didn't include any code here, as this looks like a learning exercise. Thank you for not just giving me the code, I want to learn :. I am trying to think of ways to add the numbers together with each iteration but I cannot wrap my head around a way to do it. I have figured it out!
Basically its a continuous loop that allows you to keep putting in numbers and get the correct converted measurement. I want to put an IF statement that will allow the user to type "end" to end the program instead of converting more units of measurement. How would I go about making this work what python code allows you to exit a written program and can be used in an IF statement.
To end the loop, you can use a break statement. This can be used inside an if statement, since break only looks at loops, not conditionals. That's because your code has a minor problem right now: you're calling float before you ever check what the user typed.
Which means if they type "end", you'll call float "end" and get an exception.
How to use Loops in Python
Change your code to something like this:. One more improvement you can make: if you want the user to be able to type "END" or "End" or "end", you can use the lower method to convert the input to lowercase before comparing it:. Just use iter with a sentinel:. Note that you probably want some error checking in case the user enters a non numeric value like in the example above. To end loop you could use break statement. You could also take advantage of fact that ValueError is raised if non convertible value is issued:.
Or if you want to exit only on string exit and go to next loop if error occurs, nice way would be:. Testing for MM after it is input by the user could work. You use the keyword break to break out of the loop.
Your example, after the minor additions is as follows. Learn more. How to use user input to end a program? Ask Question. Asked 6 years, 11 months ago. Active 6 years, 11 months ago. Viewed 19k times. Py Inhale.
Py 1 1 gold badge 5 5 silver badges 14 14 bronze badges. You can use a break statement to stop a while loop. Your code already works. When I enter endthe program ends.
I have a function that evaluates input, and I need to keep asking for their input and evaluating it until they enter a blank line. How can I set that up? Note that if you are on Python 3. This is a small program that will keep asking an input until required input is given. Learn more. Python: How to keep repeating a program until a specific input is obtained?
Asked 6 years, 4 months ago. Active 11 months ago. Viewed 93k times. Any help? How does it not work? What does it do? What do you expect to see? That above code repeatedly asks for the input if it is not ''.Python While loop 3
I need it to evaluate input until a blank line is entered. Active Oldest Votes. There are two ways to do this. Shahid Ghafoor Shahid Ghafoor 4 4 silver badges 12 12 bronze badges. Actually, it keeps repeating even after the required input is given. The Overflow Blog. Podcast Programming tutorials can be a real drag. Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap. Technical site integration observational experiment live on Stack Overflow.
Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Your code won't work because you haven't assigned anything to n before you first use it.
Try this:. Also input in Python 2. Note: In Python 3. Learn more. Loop until a specific user input [duplicate] Ask Question. Asked 8 years, 5 months ago. Active 1 month ago. Viewed 90k times. Gino Mempin 6, 10 10 gold badges 32 32 silver badges 38 38 bronze badges. George Burrows George Burrows 2, 9 9 gold badges 27 27 silver badges 30 30 bronze badges. Active Oldest Votes. Fred Foo Fred Foo k 61 61 gold badges silver badges bronze badges.
I think the use of input here is also wrong. From the looks of the print statements, the OP is using Python 2. Could either of you tell me why the code now terminates after entering True or False?
What exactly did you think would happen when it hit the break statement?
Perhaps you should read a Python tutorial Mark Byers Mark Byers k gold badges silver badges bronze badges.Get the latest tutorials on SysAdmin and open source topics. Write for DigitalOcean You get paid, we donate to tech non-profits.
DigitalOcean Meetups Find and meet other developers in your city. Become an author. Using for loops and while loops in Python allow you to automate and repeat tasks in an efficient manner.
But sometimes, an external factor may influence the way your program runs. When this occurs, you may want your program to exit a loop completely, skip part of a loop before continuing, or ignore that external factor. You can do these actions with breakcontinueand pass statements. In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered.
In this small program, the variable number is initialized at 0. Then a for statement constructs the loop as long as the variable number is less than Within the for loop, there is an if statement that presents the condition that if the variable number is equivalent to the integer 5, then the loop will break.
Within the loop is also a print statement that will execute with each iteration of the for loop until the loop breaks, since it is after the break statement. To know when we are out of the loop, we have included a final print statement outside of the for loop.
This shows that once the integer number is evaluated as equivalent to 5, the loop breaks, as the program is told to do so with the break statement. The continue statement gives you the option to skip over the part of a loop where an external condition is triggered, but to go on to complete the rest of the loop.
That is, the current iteration of the loop will be disrupted, but the program will return to the top of the loop. The continue statement will be within the block of code under the loop statement, usually after a conditional if statement. The difference in using the continue statement rather than a break statement is that our code will continue despite the disruption when the variable number is evaluated as equivalent to 5.
Here, Number is 5 never occurs in the output, but the loop continues after that point to print lines for the numbers before leaving the loop. You can use the continue statement to avoid deeply nested conditional code, or to optimize a loop by eliminating frequently occurring cases that you would like to reject. The continue statement causes a program to skip certain factors that come up within a loop, but then continue through the rest of the loop.Get the latest tutorials on SysAdmin and open source topics.
Write for DigitalOcean You get paid, we donate to tech non-profits. DigitalOcean Meetups Find and meet other developers in your city. Become an author. One way to repeat similar tasks is through using loops.
Subscribe to RSS
A while loop implements the repeated execution of code based on a given Boolean condition. The code that is in a while block will execute as long as the while statement evaluates to True.
You can think of the while loop as a repeating conditional statement. After an if statement, the program continues to execute code, but in a while loop, the program jumps back to the start of the while statement until the condition is False. The something that is being done will continue to be executed until the condition that is being assessed is no longer true. While going through this loop, there are two possible outcomes:.
Here, the while is followed by the variable password. This means that if the user inputs the string passwordthen the loop will stop and the program will continue to execute any code outside of the loop.
However, if the string that the user inputs is not equal to the string passwordthe loop will continue. Inside of the while loop, the program runs a print statement that prompts for the password. The program will check to see if the variable password is assigned to the string passwordand if it is, the while loop will end.
The last print statement is outside of the while loop, so when the user enters password as the password, they will see the final print statement outside of the loop. However, if the user never enters the word passwordthey will never get to the last print statement and will be stuck in an infinite loop. An infinite loop occurs when a program keeps executing within one loop, never leaving it. Here is sample output from the program:. Keep in mind that strings are case sensitive unless you also use a string function to convert the string to all lower-case for example before checking.
To best understand how this program works, you should also read about using conditional statements and converting data types.While loops are really useful because they let your program run until a user decides to quit the program. They set up an infinite loop that runs until the user does something to end the loop. This section also introduces the first way to get input from your program's users. A while loop tests an initial condition. If that condition is true, the loop starts executing. Every time the loop finishes, the condition is reevaluated.
As long as the condition remains true, the loop keeps executing. As soon as the condition becomes false, the loop stops executing. Here is a simple example, showing how a game will stay active as long as the player has enough power. Almost all interesting programs accept input from the user at some point. You can start accepting user input in your programs by using the input function.
The input function displays a messaget to the user describing the kind of input you are looking for, and then it waits for the user to enter a value. When the user presses Enter, the value is passed to your variable.
You need a variable that will hold whatever value the user enters, and you need a message that will be displayed to the user. In the following example, we have a list of names. We ask the user for a name, and we add it to our list of names. In Python 3, you always use input. In Python 2. The function input will work in Python 2. When you use the input function in Python 2.
This is fine in controlled situations, but it's not a very safe practice overall. If you're using Python 3, you have to use input. If you're using Python 2.
Most of the programs we use every day run until we tell them to quit, and in the background this is often done with a while loop. Here is an example of how to let the user enter an arbitrary number of names. That worked, except we ended up with the name 'quit' in our list.
We can use a simple if test to eliminate this bug:. This is pretty cool! We now have a way to accept input from users while our programs run, and we have a way to let our programs run until our users are finished working. You now have enough Python under your belt to offer users a set of choices, and then respond to those choices until they choose to quit. Let's look at a simple example, and then analyze the code:.