User input And while loop for Python

Source: Internet
Author: User
Tags sublime text

1. How function input () works

The function input () pauses the program and waits for the user to enter some text. After the user input is obtained, Python stores it in the
A variable in order to make it easier for you to use.

(1) The value can be obtained by using the Int () function

(2) modulo operator (%) divides two numbers and returns the remainder

Introduction to 2.while Loops

(1) The For loop is used for a block of code for each element in the collection, while the while loop runs continuously until the specified bar
The item is not satisfied.

Example

While condition:

code block

(2) Use Mark

In a program that requires many conditions to be satisfied before it continues to run, you can define a variable that determines whether the entire program is in
Active state. This variable is called a sign and acts as a traffic light for the program. You can allow the program to continue shipping when the flag is true
Line, and causes the program to stop running when any event results in a flag with a value of false. Thus, in the while statement, you only need to examine a
Condition--whether the current value of the flag is true, and all tests (whether or not events that should have the flag set to False)
are placed elsewhere, making the program more uncluttered.

(3) Use break to exit the loop

To exit the while loop immediately, no longer runs the rest of the code in the loop, and regardless of the result of the condition test, you can use the
Break statement. The break statement is used to control which lines of code are executed and which line of code is not
So that the program executes the code you want to execute according to your requirements.

(4) Use continue in the loop

To return to the beginning of the loop and decide whether to continue the loop based on the test result of the condition, use the Continue statement, which
Unlike the break statement, the rest of the code is no longer executed and the entire loop exits.

(5) Avoid infinite loops

Every programmer will occasionally write an infinite loop due to carelessness, especially if the exit condition of the loop is subtle.
If the program is stuck in an infinite loop, you can press CTRL + C, or you can close the terminal window that displays the program output.

To avoid writing an infinite loop, be sure to test each while loop to make sure it ends as expected. If you hope
The program ends when the user enters a specific value, runs the program and enters such a value, and if the program does not end in this case,
Please check the way the program handles this value, confirming that there is at least one such place where the loop condition is false or the break
Statement can be executed.

Attention:

Some editors (such as sublime Text) have output Windows Embedded, which can make it difficult to end an infinite loop, so it is not
The editor is not closed to end the infinite loop.

3. Using a while loop to work with lists and dictionaries

(1) Moving elements between lists

# First, create a list of users to verify
# and an empty list to store authenticated users
Unconfirmed_users = [' Alice ', ' Brian ', ' Candace ']
Confirmed_users = []
# Verify each user until no users are authenticated
# move each validated list to the list of authenticated users
While Unconfirmed_users

Current_User = Unconfirmed_users.pop ()
Print ("Verifying User:" + current_user.title ())
Confirmed_users.append (Current_User)
# Show All Authenticated Users
Print ("\nthe following users have been confirmed:")
For Confirmed_user in Confirmed_users:
Print (Confirmed_user.title ())

Run results

Verifying User:candace
Verifying User:brian
Verifying User:alice
The following users have been confirmed:
Candace
Brian
Alice

(2) Delete all list elements that contain a specific value

We use the function remove () to remove a specific value from the list, which is possible because you want to delete
Value appears only once in the list. What if you want to delete all the elements in the list that contain a specific value?
Suppose you have a pet list that contains multiple elements with a value of ' cat '. To remove all these elements, you can continue to ship
Line a while loop until the list no longer contains the value ' cat '

(3) Use user input to populate the dictionary

Use the While loop to prompt the user to enter any number of information.

User input And while loop for Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.