Getting started with Python-recognize while loops and use

Source: Internet
Author: User
Tags terminates

The content of this section

1. Recognize while loop
2. The use of the while loop
3. Nested loop: Loop sleeve loop

1. Recognize while loop

Break # terminates the current loop

Continue #跳出当次循环

1  While judging condition:   # when the while satisfies the condition, the program will always loop 2     execution Statement ... 3 else:   # when the normal loop completes, the program does the else note: break terminates the while loop, so the program does not do else , and continue is skipping cycles, so the program will execute the else4     Execution statement ...

2. The use of the while loop

The user enters a value to guess the correct age

1Age = 602  whileTrue:3guess_age = Int (input ("Age:"))4     ifGuess_age = =Age :5         Print("Congratulations to you! Guess right! ")6          Break7     elifGuess_age >Age :8         Print("guess it's big! ")9     Else:Ten         Print("guess it's small! ") One Else: A     Print("End")

3. Nested loop: Loop sleeve loop

(1) using the # number to output a rectangle, the user can specify width and height. Columns such as: Length 3, height is 4, output a horizontal with 3 # number, vertical with 4 # Number of rectangles

1height = Int (input ("Height:"))2width = Int (input ("Width:"))3NUM1 = 14  whileNUM1 <=Height:5num2 = 16      whileNum2 <=Width:7         Print("#", end="")8Num2 + = 19NUM1 + = 1Ten     Print() One Else: A     Print("End")

(2) Input "99 multiplication table" with while loop

1NUM1 = 12  whileNUM1 <= 9:3num2 = 14      whileNum2 <=NUM1:5NUM3 = num2 *NUM16         Print(NUM1,"*", Num2,"=", num3, end="   ")7Num2 + = 18     Print()9NUM1 + = 1Ten Else: One     Print("99 multiplication Table")

Getting started with Python-recognize while loops and use

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.