流程控制之while循环spa
一、什么是while循环code
循环指的是一个重复作某件事的过程blog
二、为什么有循环input
为了让计算机能像人同样重复 作某件事class
三、如何用循环循环
1 ''' 2 # while循环的语法:while循环又称为条件循环,循环的次数取决于条件 3 ''' 4 while 条件: 5 子代码1 6 子代码2 7 子代码3 8 ''' 9 # print('start....') 10 # while True: 11 # name=input('please your name>>: ') 12 # pwd=input('please your password>>: ') 13 # if name == 'egon' and pwd == '123': 14 # print('login successful') 15 # else: 16 # print('user or password err') 17 # print('end...') 18 19 # 如何结束while循环 20 # 方式一:操做while循环的条件让其结束 21 # print('start....') 22 # tag=True 23 # while tag: 24 # name=input('please your name>>: ') 25 # pwd=input('please your password>>: ') 26 # if name == 'egon' and pwd == '123': 27 # print('login successful') 28 # tag=False 29 # else: 30 # print('user or password err') 31 # 32 # print('end...') 33 34 # 方式二: break强行终止本层循环 35 # count=1 36 # while count < 6: 37 # print(count) 38 # count+=1 39 40 41 # count=1 42 # while True: 43 # if count > 5: 44 # break 45 # print(count) 46 # count+=1 47 48 49 # print('start....') 50 # while True: 51 # name=input('please your name>>: ') 52 # pwd=input('please your password>>: ') 53 # if name == 'egon' and pwd == '123': 54 # print('login successful') 55 # break 56 # else: 57 # print('user or password err') 58 # 59 # print('end...') 60 61 62 # 输错三次则退出 63 # 方式一: 64 # print('start....') 65 # count=0 66 # while count <= 2: #count=3 67 # name=input('please your name>>: ') 68 # pwd=input('please your password>>: ') 69 # if name == 'egon' and pwd == '123': 70 # print('login successful') 71 # break 72 # else: 73 # print('user or password err') 74 # count+=1 75 # 76 # print('end...') 77 78 79 # 方式二 80 # print('start....') 81 # count=0 82 # while True: 83 # if count == 3: 84 # print('输错的次数过多傻叉') 85 # break 86 # name=input('please your name>>: ') 87 # pwd=input('please your password>>: ') 88 # if name == 'egon' and pwd == '123': 89 # print('login successful') 90 # break 91 # else: 92 # print('user or password err') 93 # count+=1 94 # 95 # print('end...') 96 97 98 # while+continue:continue表明结束本次循环,直接进入下一次 99 # count=1 100 # while count < 6: 101 # if count == 4: 102 # count+=1 103 # continue # 只能在cotinue同一级别以前加代码 104 # print(count) 105 # count+=1 106 # 107 # 108 # while True: 109 # print('11111') 110 # print('22222') 111 # print('333') 112 # continue # 不该该将continue做为循环体最后一步执行的代码 113 114 115 # while+else 116 # count=1 117 # while count < 6: 118 # if count == 4: 119 # break 120 # print(count) 121 # count+=1 122 # else: 123 # print('会在while循环没有被break终止的状况下执行') 124 125 126 127 # 输错三次则退出之while+else的应用 128 # print('start....') 129 # count=0 130 # while count <= 2: #count=3 131 # name=input('please your name>>: ') 132 # pwd=input('please your password>>: ') 133 # if name == 'egon' and pwd == '123': 134 # print('login successful') 135 # break 136 # else: 137 # print('user or password err') 138 # count+=1 139 # else: 140 # print('输错的次数过多') 141 # 142 # print('end...') 143 144 145 146 # while循环的嵌套 147 # name_of_db='egon' 148 # pwd_of_db='123' 149 # print('start....') 150 # count=0 151 # while count <= 2: #count=3 152 # name=input('please your name>>: ') 153 # pwd=input('please your password>>: ') 154 # if name == name_of_db and pwd == pwd_of_db: 155 # print('login successful') 156 # while True: 157 # print(""" 158 # 1 浏览商品 159 # 2 添加购物车 160 # 3 支付 161 # 4 退出 162 # """) 163 # choice=input('请输入你的操做: ') #choice='1' 164 # if choice == '1': 165 # print('开始浏览商品....') 166 # elif choice == '2': 167 # print('正在添加购物车....') 168 # elif choice == '3': 169 # print('正在支付....') 170 # elif choice == '4': 171 # break 172 # break 173 # else: 174 # print('user or password err') 175 # count+=1 176 # else: 177 # print('输错的次数过多') 178 # 179 # print('end...') 180 181 182 183 184 # tag控制全部while循环 185 name_of_db='egon' 186 pwd_of_db='123' 187 tag=True 188 print('start....') 189 count=0 190 while tag: 191 if count == 3: 192 print('尝试次数过多') 193 break 194 name=input('please your name>>: ') 195 pwd=input('please your password>>: ') 196 if name == name_of_db and pwd == pwd_of_db: 197 print('login successful') 198 while tag: 199 print(""" 200 1 浏览商品 201 2 添加购物车 202 3 支付 203 4 退出 204 """) 205 choice=input('请输入你的操做: ') #choice='1' 206 if choice == '1': 207 print('开始浏览商品....') 208 elif choice == '2': 209 print('正在添加购物车....') 210 elif choice == '3': 211 print('正在支付....') 212 elif choice == '4': 213 tag=False 214 215 else: 216 print('user or password err') 217 count+=1 218 219 print('end...')