Python's while loop

First, use a while loop

  for loop for a block of code for each element of the set, and the while loop can continue to loop until the until the specified condition is not satisfied.

current_number = 0
while current_number <= 5:
    print(str(current_number))
    current_number += 1

console:

Code explanation: First we define the variable assignment is a 0, then enters a while loop, it is determined whether or less current_number 5, 0 in this case, the loop is entered print 0,

Continue down the line of code, current_number + = 1 is understood to current_number = current_number + 1, the equal sign is the assignment operator, need to be considered to the right, then the right calculated

Value is copied to the left, so current_number = 0 + 1 => current_number = 1, then determines from the start of the cycle is less than or equal to 5 CURRENT_NUMBER, case 1,

Conditions are met, continue down, and so forth, until the condition is not satisfied, the program stops.

Second, let the user choose when to quit 

  In fact, very often requires the user to program their own opt-out, then how to do it, you can refer to the following demo:

  

message = '\nTell me something,I will repeat it back to you .'
message += "\n Enter 'quit' to end the program."
user_input = ''
while user_input != 'quit':
    user_input = input(message)
    print(user_input)

 

After the run there will be shown in the interface:

We first entered the joker, the program prints a joker, we once again enter the jack, the program prints the jack, and if this time we enter quit, then quit the program.

 

Third, the use of signs to exit the loop

 In addition we exit the loop with the conditions, you can also use the flag to exit the loop, such as:

flag = True
message = ''
while flag :
    message = input("\ninput something please,enter 'quit' to end the program:")
    if message == 'quit':
        flag = False
    else:
        print('your input message is :'+message)

console:

This time, we define a flag variable flag, which is a Boolean expression, and as a condition of the while loop, flag to perform as True while loop, flag to False while loop will not be executed.

while loop, if the user input is 'quit', the flag variable is assigned flag is False, while loop determines again whether the flag value, to False, it will not be executed while loop statement, the program ends.

Fourth, the use break to exit the loop

 

flag = True
message = ''
while flag :
    message = input("\ninput something please,enter 'quit' to end the program:")
    if message == 'quit':
        break
    else:
        print('your input message is :'+message)

console:

将flag = False 改为 break ,程序执行效果是一样的,break 的意思是结束当前循环,继续执行循环后的语句,而本例子中,循环后没有其他语句,所以程序结束。

五、在循环中使用 continue

 

num = 0 
while num <= 10:
    num += 1
    if num % 2 != 0:
        continue
    else:
        print(num)
    

console:

本例子是打印了1--10之内(包含10)的偶数,% 符号 表示取模,取模的意思:把它当做除号,然后算余几,这个几就是模,比如:3%2 的模为1因为3除以2等于1 余 1,所以它的模为1。

continue:与break不同的是continue只是跳出本次循环,如果循环条件还是满足的话,它会继续执行循环里面的代码块;break则是:整个循环都不执行了,就算循环条件满足也不执行这个循环了。

六、避免出现无限循环

   我们在编写代码时应该,避免出现死循环即无限循环,也就是条件永远满足的状态,因为无限循环是非常占计算机性能的,如果控制台出现了无限循环,可以使用ctrl + c 终止程序。

 


 

好看的锁骨千篇一律,有趣的肚腩弹来弹去。

 

Guess you like

Origin www.cnblogs.com/tizer/p/10958018.html