A short story, play Python-while loop

Whether traditional or contemporary popular programming scenarios artificial intelligence application scenarios, the application cycle is essential, the article explains how to use a for loop to program, this article will often hear of a child's the story in terms of the while loop Python programming.

The wolf comes knocking

Little Rabbit obediently story we have heard, vaguely remember the big bad wolf comes knocking, small rabbits do not open the door, my mother came back, put a small rabbit children open the door opened. So we use the program to describe the process of how to do?

Before you start writing code, we first look at the basic syntax of the while loop:

while 判断条件是否成立,如果成立,那么开始执行循环:
执行循环中的具体代码

From grammar, we can see that we need to clearly determine the conditions, according to the scenario described in the story, in our judgment condition is that the mother came back, little rabbit will open the door, otherwise the little rabbit is not going to open the door.

First we have to learn a input function, which means that you want the user to enter a text in the terminal, then how do we use it?

input("你是谁?")

Run this code, we can get to the name entered by the user

Did you hear a story like talking about the same programming it?

Run the sample

In order to be able to be judged according to the following name entered by the user, we will adopt such an approach below, we use the name variable to catch user input, that is, the user input is assigned to the variable name

name = input("你是谁?")

Here we continue to sort out the logic, when the input is not the name of the mother, the door is closed, which means that the cycle will continue only if the name is Mom, the door will open, the end of the cycle, stop running the code. According to this logic we continue to write code

# 首先我们声明一个变量名字,让用户先输入一次
name = input("你是谁?")
# 判断名字不等妈妈的时候我们继续循环
while name!="妈妈":
print("门关着,妈妈没回来")
# 这里让用户继续输入名字是谁
name = input("你是谁?")
# nama等于了"妈妈",循环结束
print("门开了,妈妈回来了")

Since we are at the command line, so the code must be entered segments, or else not, the following code, the first time I let the name equal to an empty string, in fact, is the same, everyone in practice time can try.

Did you hear a story like talking about the same programming it?

Since the code we are now entered on the command line, you can only enter segments

If on top of that there is so little about the logic, then we look at the next way to write, writing in a positive judgment

# 首先我们把循环判断一直写成真的,也就是通常所说的死循环
while True:
# 这里让用户输入一个名字
name = input("你是谁?")
# 判断用户输入的名字是否等于妈妈
if name == "妈妈":
# 打印一下我们想要的结果
print("妈妈回来了,门开了")
# break的意思就是跳出循环
break
print("妈妈没回来,不开门")

Did you hear a story like talking about the same programming it?

 

This is the use of the while loop is very simple, but very common

Big week of house technology, and strive to one day a technical point, bring you a taste of the fun of programming.

Guess you like

Origin www.cnblogs.com/lingfengblogs/p/11093312.html
Recommended