In Python, it also has this meaning, but it's a little different, "when ... "This condition is set at a range or time interval, allowing Python to do a lot of things during this time interval." It's like this scenario:
While older than 60 years:--------> When the age is greater than 60 years old
Retirement--------> Any action performed in accordance with the above conditions
Imagine, if the production of a door, this door is to use the above conditions to regulate the switch, assuming that a lot of people through this, reported age, as long as the age of more than 60, on retirement (door open, people can go out), one by one to cycle down, suddenly there is a person age is 50, then this cycle in his stop, That is when he was not satisfied with the condition.
This is the while loop. To write a serious process, you can look at:
Guess the number game again.
This tutorial has a talk, is with crossing to do a small game, in the inside to do a guessing game, at that time encountered a problem, is can only guess one or two times, if not guessed, the program can not continue to run.
Not long ago, there is a university student friend (his name Hangyuan Li), send me e-mail, let me see his game, can achieve multiple guesses, until the guess until the right. This is a college student who likes to study.
I am here to write his program in this, unit Hangyuan Li classmate don't take offense, if Hangyuan Li classmate think this infringement of their intellectual property, can tell me, I immediately withdraw this code.
Copy the Code code as follows:
#! /usr/bin/env python
#coding: UTF-8
Import Random
I=0
While I < 4:
print ' ******************************** '
num = input (' Please enter any number from 0 to 9: ') #李同学用的是python3
Xnum = Random.randint (0,9)
x = 3-i
if num = = Xnum:
print ' It's good luck, you guessed it! '
Break
Elif num > Xnum:
Print "You guessed big!\n haha, the correct answer is:%s\n You also have%s chance! "% (xnum,x)
Elif Num < xnum:
Print "You guessed small!\n haha, the correct answer is:%s\n You also have%s chance! "% (xnum,x)
print ' ******************************** '
i + = 1
We use this procedure to analyze, first look at while I<4, which is the program in order to guess limit the number of times, the maximum is three times, please crossing note, in the while loop body in the last sentence: I +=1, which is said each cycle to the end, will give I 1, when bool (i<4) = When false, it is no longer looped.
when bool (i<4) =true, it executes the statement in the loop body. In the loop body, let the user enter an integer, then the program randomly selects an integer, and finally determine whether the number of randomly generated and the number of user input is equal, and the IF statement to determine three different situations.
According to the above code, crossing see if it can be modified?
In order to make the user's experience more refreshing, you may want to enlarge the input integer range between 1 and 100.
Copy the Code code as follows:
Num_input = raw_input ("Please input one ' integer that was in 1 to:") #我用的是python2.7, which is different from the students in the input instructions
The program receives the input content with the Num_input variable. However, please yours faithfully crossing must pay attention, see here want to sleep to play up the spirit, I want to share a years of programming experience, remember: Any user input content is unreliable. This sentence has a profound meaning, but there is not much to explain here, and you will need to experience it in a subsequent programming career. To do this, we want to verify that the user input is in line with our requirements, we require the user to enter an integer between 1 and 100, then do the following test:
Whether the input is an integer
If it is an integer, whether it is between 1 and 100.
To do this, do:
Copy the Code code as follows:
If not Num_input.isdigit (): #str. IsDigit () is used to determine whether a string is purely composed of numbers
Print "Please input interger."
elif Int (num_input) <0 and int (num_input) >=100:
Print "The number should is in 1 to 100."
Else
Pass #这里用pass, meaning temporarily omitted, if the preceding request is fulfilled, the statement is executed here
Then look at Hangyuan Li Classmate's program, in the loop to produce a random number, so that the user each time input, the face is a new random number. This kind of guessing number game is too difficult. I hope that the program will produce a number until the guess is the number. So, to move the command that generates the random number before the loop.
Copy the Code code as follows:
Import Random
Number = Random.randint (1,100)
While True: #不限制用户的次数了
...
Observe Li Classmate's program, there is a point need to show to yours faithfully, that is in the conditional expression, the two sides preferably the same type of data, the above program has: Num>xnum style of the conditional expression, and one side is the program generated int type data, one side is the input function to get the STR type data. In some cases you can run, why? Can crossing understand? When it's all numbers, it's possible. But this is not good.
So, according to this idea, rewrite this guessing program:
Copy the Code code as follows:
#!/usr/bin/env python
#coding: Utf-8
Import Random
Number = Random.randint (1,100)
Guess = 0
While True:
Num_input = raw_input ("Please input one integer," in 1 to 100: ")
Guess +=1
If not Num_input.isdigit ():
Print "Please input interger."
elif Int (num_input) <0 and int (num_input) >=100:
Print "The number should is in 1 to 100."
Else
If Number==int (num_input):
Print "OK, you are good. It's only%d and then you successed. " %guess
Break
Elif Number>int (num_input):
Print "Your number is more than less."
Elif number<>
Print "Your number is bigger."
Else
Print "There is something bad, I won't work"
The above for reference, crossing can also be improved.
Break and Continue
Break, which has already appeared in the above example, is meant to interrupt the loop in this place and jump out of the loop body. The following brief example is more obvious:
Copy the Code code as follows:
#!/usr/bin/env python
#coding: Utf-8
A = 8
While a:
If a%2==0:
Break
Else
Print "%d is odd number"%a
A = 0
Print "%d is even number"%a
A=8, execute the break in the loop body, jump out of the fantasy, execute the final print statement, and get the result:
Copy the Code code as follows:
8 is even number
If a=9, then the print in the else is executed, and then a=0, the loop executes once, then break, to get the result:
Copy the Code code as follows:
9 is odd number
0 is even number
The Continue is to jump from the current position (that is, the location of the continue) to the end of the last line of the loop body (not the last line), for a loop body, as the end of the convergence, the last line behind what is it? Of course it's the beginning.
Copy the Code code as follows:
#!/usr/bin/env python
#coding: Utf-8
A = 9
While a:
If a%2==0:
A-=1
Continue #如果是偶数, return to the beginning of the loop
Else
Print "%d is odd number"%a #如果是奇数.
A-=1
In fact, for these two things, I personally rarely used in programming. I have a stubborn concept, try to put the conditions before the cycle to do enough, do not jump in the loop to jump, not only the readability of the decline, sometimes their own confused.
Copy the Code code as follows:
While...else
These two mates are a bit like if ... else, just one example is understood, and of course, when you encounter the else, it means that it's not inside the while loop.
Copy the Code code as follows:
#!/usr/bin/env python
Count = 0
While Count < 5:
Print count, "is less than 5"
Count = Count + 1
Else
Print count, "is isn't less than 5"
Execution Result:
Copy the Code code as follows:
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is isn't less than 5