day04流程控制之while循環

                             流程控制之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...')
相關文章
相關標籤/搜索