Day 5 while loop for loop

Why use a loop structure?

Let the computer keep repeating something like a human

while loop (conditional loop)

语法
while 条件:
代码1
代码2
代码3

Steps of while loop operation:

步骤1:如果条件为真,那么依次执行:代码1,代码2,代码3...
步骤2:执行完毕后循环到while条件,再次判断条件:
如果条件为True,则再次执行:代码1,代码2,代码3...
如果条件为False,则循环终止

The efficiency of the infinite loop:

while True:
	1+1
纯计算无io死循环会导致致命的效率问题

The way to exit the loop:
change the while condition definition:

先定义A=Ture,当条件为while =A时,可以继续循环子代码,执行代码完毕后,如果条件中A=False。
那么再次循环时,刚开头中的条件while=A就变成了while=False。那么判断条件为False,停止循环。

案例:
A = True
while A:
    name = input("猜名字:")
    if name == "nana":
        print("恭喜你答对了")rue
        A = False
    a += 1

while+break

只要运行到break就会立刻终止本层循环
while True:
    name = input("猜名字:")
    if name == "nana":
        print("恭喜你答对了")
        break
    a += 1

Nested multi-level while loop

方式一:
A=Ture
while A:
	while A:
		while A:
			A=False

Way two:

while True:
	while True:
		while True:
			break
		break
	break
案例1:
 tag = True
 while tag:
     inp_user = input("username>>>: ")
     inp_pwd = input("password>>>: ")

     if inp_user == "nana" and inp_pwd == "123":
         print('login successful')
         # break
         tag = False
     else:
         print('username or password error')

     # print('=======================>')
案例2
 while True:
     inp_user = input("username>>>: ")
     inp_pwd = input("password>>>: ")
     if inp_user == "nana" and inp_pwd == "123":
         print('login successful')
         while True:
             print("""
             0 退出
             1 取款
             2 存款
             3 转账
             """)
             choice = input("请输入您的命令编号:")
             if choice == "0":
                 break
             elif choice == "1":
                 print("正在取款。。。")
             elif choice == "2":
                 print("正在存款。。。")
             elif choice == "3":
                 print("正在转账。。。")
             else:
                print("输入的指令不存在")
         break
    else:
         print('username or password error')

while+continue:

结束本次循环,直接进入下一次循环
注意:同级别之后千万别写代码,写了也不会运行,不要把continue加在最后一步,因为while循环运行到最后一步,
会自动跳转到while 开头进行continue的操作。

while + elseelse的子代码块会在while循环正常死亡时运行,没有被break干掉的就叫正常死亡
 
 count = 0
 while count < 5:
     if count == 2:
          count += 1
          continue		# 打印0,1,3,4 =====>
          #break	 # 打印0,1
     print(count)
     count += 1
 else:
     print('====>')

for loop:

B=[1,2,3]
for A in B:表示把B的值赋值给A
	print(A) #值为123

for x in "hello":
     print(x) #值为 h  e  l   l  o

The for loop takes the value of the dictionary. Usage :

a = {
    
    "k1": 111, "k2": 222, "k3": 333}
for x in a:
    print(x)  # k1 k2 k3 默认取的是key值

a = {
    
    "k1": 111, "k2": 222, "k3": 333}
for x in a:
    print(a[x])     # 111 222 333 x取出来的是key值,a[x]相当于通过key值取value值

a = {
    
    "k1": 111, "k2": 222, "k3": 333}
for x in a.items():
    print(x)       # ("k1",111) ("k2",222) ("k3",333)
    
a = {
    
    "k1": 111, "k2": 222, "k3": 333}
for x,y in a.items():
    print(x,y)       # 通过解压赋值 把元组里面的值取出来k1 111     k2 222      k3 333
  	
a = {
    
    "k1": 111, "k2": 222, "k3": 333}
for x in a.values():
    print(x)    # 111  222  333

Usage of for loop to take the value of the list:

a = [["a", 1], ["b", 5], ["s",2], ["p", 2]]
for i in a:
	print(i)       # 值为  ["a", 1]     ["b", 5]    ["s",2]     ["p", 2]
for x,y in a:
	print(x,y)      #值为a 1     b 5      s  2      p2
这里用到了解压赋值的放法
思路:i = x ,y        #print(i)=print(x,y)

Some usages of for loop plus conditions

 for+break
for i in [11,22,33,44,55]:
    if i == 33:
        break
      print(i)
 for+continue
 for i in [11,22,33,44,55]:
    if i == 33:
        continue
     print(i)
for+else
for i in [11,22,33,44,55]:
     if i == 33:
         break
     print(i)
 else:
     print('++++++++>')

for+range: to make up for the shortcomings of for repeated operations

例:
x = 0
while x < 10000:
print(111)
x += 1
 
等同于:
 
for x in range(10000):
print(111)

Guess you like

Origin blog.csdn.net/Yosigo_/article/details/111500939