1.5 Python Basics-While loop

Source: Internet
Author: User

There are a lot of things that we have to do in our lives, or actions, that we call loops. There will also be loops in the development process, that is, the need to execute a code repeatedly, or to perform some kind of calculation over and over again, until a certain condition is reached. In Python we are using the while loop.

Pseudo code:

  when The condition is established:

Execution Loop Body

Until the conditions are not set.

Code format:

 while  ...:    ...

Example code:

n = 0 while n <:        = n + 1        #  or n + = 1        print(n)

# Execution Results

1
2
3
4
5
6
7
8
9
10

In the sample code, we will assign an n to an initial value of 0 and increment the N in the while loop body until the n<10 is not true, that is, N is greater than 10 to end the loop. In the loop, we must be careful to have an end condition, do not create an infinite loop, that is, the cycle of death.

Let's look at one more example of a guess number code:

Number = 47 whileTrue:guess_number= Input ("Please enter the number you want to guess:")    #because the received parameter through input is a string, we need to convert the string to a number before we can compare the operationGuess_number =Int (guess_number)ifGuess_number = =Number :Print("Congratulations, you guessed it! ")         Break    elifGuess_number >Number :Print("guess it's too big! ")    Else:        Print("I guess it's too small! ")

In the Guess number example code, we assign the variable number 47, where the loop content is repeatedly asked the user to enter a number, to compare, the loop ends the condition is: Guess_number equals numbers, and jumping out of the loop is controlled by the loop control word break. The words that control loops in the loop have continue in addition to break. Break, is to jump out of the entire loop, continue, is to end the current loop, and proceed to the following loop content.

Continue example:

n = 0 while n<10:    + = 1    if n = = 6:        continue    Print (n)

# Execution Results

1
2
3
4
5
7
8
9
10

In this example, it can be seen that the loop body to n constant self-increment operation, and print out the value of N, when n equals 6, encountered continue, jumped out of the current loop, did not print out, continue to execute the rest of the loop content, until the condition is not satisfied.

Guess the number we can modify, in a certain number of times to guess, limit the number of guesses.

Number = 47Count=0 whileCount < 3: Guess_number= Input ("Please enter the number you want to guess:")Guess_number =Int (guess_number)ifGuess_number = =Number :Print("Congratulations, you guessed it! ")         Break    elifGuess_number >Number :Print("guess it's too big! ")    Else:        Print("I guess it's too small! ") Count+=1

We add one more condition, when the number of guesses arrives 3 times, exits the loop, and informs the exit reason.

Number = 47Count=0 whileCount < 3: Guess_number= Input ("Please enter the number you want to guess:"Guess_number=Int (guess_number)ifGuess_number = =Number :Print("Congratulations, you guessed it! ")         Break    elifGuess_number >Number :Print("guess it's too big! ")    Else:        Print("I guess it's too small! ") Count+=1Else:    Print("I'm sorry. You've guessed too many times! ")

Here we just add the else code block, here means that when the loop in the while, the normal loop is complete, while the loop condition is not established (that is, count is greater than or equal to 0), no break and end, will execute the code block!

1.5 Python Basics-While loop

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.