While loop calculation rules: inner loop-outer loop !, While rule

Source: Internet
Author: User

While loop calculation rules: inner loop-outer loop !, While rule

Num = 1 # value = 1
While num <= 10: # num (1) less than 10
Print (num) # The value of this 1 should be printed
Num + = 1 # num + = 1 is equivalent to num plus 1, so this value is 2 in total.
If num = 6: # if the program is terminated for 6, if it is not up to 6, the program will go over again until it reaches 6.
Break
For example:
While num <= 10: if the value of num is 1, print 1
But in the place where mum + = 1 is executed, the value of num is added to 2
But it still does not meet the conditions for terminating the program, so the second round is required.

If we know that the value of the last num in the first round is 2, the arrival of num + = 1 will change to 3, but the termination condition is still not met,
In this case, we will continue to output the 4 and 5 items according to the above method and stop them automatically until 6.
So the final result is 12345.

 

While variable Determination
Age = 18 # Set the variable to 18
Sign = True # The sige variable is equal to the (True) criterion.
While sign: # while sign: Definition
Guess = int (input ("guess_age:") # guess = data filled in by the user interactive Device
If guess = age: # if the guess variable is equal to the age variable
Print ("Yes Good! ") # Print yes
Sign = False # If sign is equal to (False), the condition cannot be executed.
Elif guess <age: # If guess is smaller than age
Print ("you need to enter one bingger! ") # Print your need to neter one bigger
Else: # Otherwise
Print ("you need to enter one smaller! ") # Print you need to enter one smaller!
Print ("end! ") # End!

 


Break termination Determination

Age = 18
While True: # When the decision is True
Guess = int (input ("guess_age :"))
If guess = age: # if the guess value is equal to age
Print ("Yes Good! ") # Yes good!
Break # and terminate!
Elif guess <age: # Otherwise, if guess is smaller than age
Print ("you need to enter one bingger! ")
Else:
Print ("you need to enter one smaller! ")
Print ("end! ")

While loop calculation rules:

 

 

 

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.