1. 雙向循環的練習
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# 1.用兩個循環完成十行十列的小星星 j = 0 while j < 10 : # 打印星星 i = 0 while i < 10 : print ( "*" ,end = "") i + = 1 # 打印換行 print () j + = 1 # 2.用兩個循環完成十行十列隔列換色的小星星 """ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ ☆★☆★☆★☆★☆★ """ i = 0 while i < 10 : # 打印一行黑白相間的星星 j = 0 while j < 10 : if j % 2 = = 0 : print ( "☆" ,end = "") else : print ( "★" ,end = "") j + = 1 # 打印換行 print () i + = 1 # 3.用兩個循環完成十行十列隔行換色的小星星 """ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ ★★★★★★★★★★ ☆☆☆☆☆☆☆☆☆☆ """ """ 外層的循環i動的慢 內層的循環j動的快 外層的i動一次, 內層的循環動10次 """ i = 0 while i < 10 : j = 0 while j < 10 : if i % 2 = = 0 : print ( "☆" ,end = "") else : print ( "★" ,end = "") j + = 1 print () i + = 1 # 4.99乘法表 # 方向一 i = 1 while i < = 9 : # 打印對應的表達式 j = 1 while j < = i: print ( "%d*%d=%2d " % (i,j,i * j) ,end = "" ) j + = 1 # 打印換行 print () i + = 1 # 方向二 i = 9 while i > = 1 : # 打印對應的表達式 j = 1 while j < = i: print ( "%d*%d=%2d " % (i,j,i * j) ,end = "" ) j + = 1 # 打印換行 print () i - = 1 print ( "<====================>" ) # 方向三 i = 1 while i < = 9 : kongge = 9 - i # 打印空格 while kongge > 0 : print ( " " ,end = "") kongge - = 1 # 打印表達式 j = 1 while j < = i: print ( "%d*%d=%2d " % (i,j,i * j) ,end = "" ) j + = 1 # 換行 print () i + = 1 print ( "<===============>" ) # 方向四 i = 9 while i > = 1 : kongge = 9 - i # 打印空格 while kongge > 0 : print ( " " ,end = "") kongge - = 1 # 打印表達式 j = 1 while j < = i: print ( "%d*%d=%2d " % (i,j,i * j) ,end = "" ) j + = 1 # 打印換行 print () i - = 1 # 求吉利數字 100 ~ 999 之間 找 111 222 333 123 456 654 321 ... """ // 可以獲取一個數高位 % 可以獲取一個數低位 baiwei = 345 // 100 shiwei = 345 // 10 % 10 gewei = 345 % 10 print(gewei) """ # 方法一 i = 100 while i < = 999 : baiwei = i / / 100 shiwei = i / / 10 % 10 gewei = i % 10 if shiwei = = gewei and shiwei = = baiwei : print (i) # 123 elif shiwei = = gewei - 1 and shiwei = = baiwei + 1 : print (i) # 987 elif shiwei = = gewei + 1 and shiwei = = baiwei - 1 : print (i) i + = 1 # 方法二 print ( "<====>" ) i = 100 while i < = 999 : strvar = str (i) # print(strvar, type(strvar)) gewei = int (strvar[ - 1 ]) shiwei = int (strvar[ 1 ]) baiwei = int (strvar[ 0 ]) if shiwei = = gewei and shiwei = = baiwei : print (i) # 123 elif shiwei = = gewei - 1 and shiwei = = baiwei + 1 : print (i) # 987 elif shiwei = = gewei + 1 and shiwei = = baiwei - 1 : print (i) i + = 1 # 方法三 print ( "<====>" ) i = 100 while i < = 999 : strvar = str (i) # print(strvar, type(strvar)) gewei = int (strvar[ - 1 ]) shiwei = int (strvar[ 1 ]) baiwei = int (strvar[ 0 ]) if 2 * shiwei = = gewei + baiwei and (shiwei = = gewei + 1 or shiwei = = gewei - 1 ): print (i) elif gewei = = shiwei and shiwei = = baiwei: print (i) i + = 1 # 百錢買百雞 # 公雞一個五塊錢,母雞一個三塊錢,小雞三個一塊錢,現在要用一百塊錢買一百只雞,問公雞、母雞、小雞各多少只? """ 窮舉法:把數據拿出來一個一個試 x = [1,2] y = [3,4] z = [5,6] x+y+z = 10 1 + 3 + 5 = 9 1 + 3 + 6 = 10 bingo 1 + 4 + 5 = 10 bingo 1 + 4 + 6 = 11 2 + 3 + 5 = 10 bingo 2 + 3 + 6 = 11 2 + 4 + 5 = 11 2 + 4 + 6 = 12 """ """ 公雞 : x 母雞 : y 小雞: z 雞的數量:x + y + z = 100 雞的價格:5 * x + 3 * y + 1/3*z = 100 """ x = 0 while x < = 20 : y = 0 while y < = 33 : z = 0 while z < = 100 : if x + y + z = = 100 and 5 * x + 3 * y + 1 / 3 * z = = 100 : print (x,y,z) z + = 1 y + = 1 x + = 1 |
2. break_pass_continue的使用
關鍵字的使用 pass break continue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# pass 過 (代碼塊中的占位符) """ if 20 == 20: pass while True: pass """ # break 終止當前循環 (只能用在循環之中) # 1 ~ 10 遇到5終止循環 i = 1 while i < = 10 : print (i) if i = = 5 : break i + = 1 i = 1 while i < = 3 : j = 1 while j < = 3 : if j = = 2 : break print (i,j) j + = 1 i + = 1 # 1 1 # 2 1 # 3 1 """ if 5 == 5: error break """ # continue 跳過當前循環,從下一次循環開始 # 打印 1 ~ 10 跳過5 i = 1 while i < = 10 : if i = = 5 : # 在跳過之前,因為會終止執行后面的代碼,從下一次循環開始 # 為了避免死循環,手動加1 i + = 1 continue print (i) i + = 1 # 1 ~ 100 打印所有不含有4的數字 # 方法一 print ( "<============>" ) i = 1 while i < = 100 : strvar = str (i) # print(strvar) if "4" in strvar: i + = 1 continue print (i) i + = 1 # 方法二 print ( "<============>" ) i = 1 while i < = 100 : if i / / 10 = = 4 or i % 10 = = 4 : i + = 1 continue print (i) i + = 1 |
3. for循環
while
: 邏輯比較復雜用while
for
: 邏輯比較簡單,遍歷數據的話一定是for(while也可以遍歷數據,但是遍歷的數據不能是無序的)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# 遍歷 循環 迭代 , 把容器中的元素一個一個獲取出來 # while循環在遍歷數據時的局限性 """ lst = [1,2,3,4,5] # ok i = 0 while i < len(lst): print(lst[i]) i+=1 setvar = {"a","b","c"} # not ok i = 0 while i < len(setvar): print(setvar[i]) i+=1 """ # for循環的基本語法 """ Iterable 可迭代性數據:1.容器類型數據 2.range對象 3.迭代器 for 變量 in Iterable: code1. """ # 字符串 container = "北京和深圳溫差大概20多度" # 列表 container = [ 1 , 2 , 3 , 4 , 4 , 5 ] # 元組 container = ( "孫開洗" , "孫健" , "孫悟空" ) # 集合 container = { "陳璐" , "曹靜怡" , "王志國" , "鄧鵬" , "合力" } # 字典 container = { "cl" : "風流倜儻" , "cjy" : "拳擊選手" , "wzg" : "尋花問柳" , "dp" : "帥氣,祖國的棟梁" , "hl" : "你是個好人" } # 遍歷數據 for i in container: print (i) print ( "<===================>" ) # 1.遍歷不等長多級容器 container = [ 1 , 2 , 3 , 4 ,( "嗄" , "234" ,{ "馬春配" , "李虎凌" , "劉子濤" })] for i in container: # 判斷當前元素是否是容器,如果是,進行二次遍歷,如果不是,直接打印 if isinstance (i, tuple ): # ("嗄","234",{"馬春配","李虎凌","劉子濤"}) for j in i: # 判斷當前元素是否是集合,如果是,進行三次遍歷,如果不是,直接打印 if isinstance (j, set ): # j = {"馬春配","李虎凌","劉子濤"} for k in j : print (k) else : print (j) # 打印數據 else : print (i) # 2.遍歷不等長多級容器 container = [( "劉玉波" , "歷史源" , "張光旭" ), ( "上朝氣" , "于朝志" ),( "韓瑞曉" ,)] for i in container: for j in i: print (j) # 3.遍歷等長的容器 container = [( "馬云" , "小馬哥" , "馬春配" ) , [ "王健林" , "王思聰" , "王志國" ],{ "王寶強" , "馬蓉" , "宋小寶" }] for a,b,c in container: print (a,b,c) # 變量的解包 a,b,c = "poi" a,b = ( 1 , 2 ) a,b = 1 , 2 a,b,c = [ 10 , 11 , 12 ] a,b = { "林明輝" , "家率先" } a,b = { "lmh" : "林明輝" , "jsx" : "家率先" } a,b,c = ( "馬云" , "小馬哥" , "馬春配" ) print (a,b,c) # ### range對象 """ range([開始值,]結束值[,步長]) 取頭舍尾,結束值本身獲取不到,獲取到它之前的那一個數據 """ # range(一個值) for i in range ( 5 ): # 0 ~ 4 print (i) # range(二個值) for i in range ( 3 , 8 ): # 3 4 5 6 7 print (i) # range(三個值) 正向的從左到右 for i in range ( 1 , 11 , 3 ): # 1 4 7 10 print (i) # range(三個值) 逆向的從右到左 for i in range ( 10 , 0 , - 1 ): # 10 9 8 7 ... 1 print (i) # 總結: """ while 一般用于處理復雜的邏輯關系 for 一般用于迭代數據 部分情況下兩個循環可以互相轉換; """ i = 1 while i < = 9 : j = 1 while j < = i: print ( "%d*%d=%2d " % (i,j,i * j) ,end = "" ) j + = 1 print () i + = 1 for i in range ( 1 , 10 ): for j in range ( 1 ,i + 1 ): print ( "%d*%d=%2d " % (i,j,i * j) ,end = "" ) print () # 打印 1 ~ 10 跳過5 i = 1 while i < = 10 : if i = = 5 : i + = 1 continue print (i) i + = 1 for i in range ( 1 , 11 ): if i = = 5 : continue print (i) |
小提示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
"".strip() 可以去掉字符串的兩邊的空白符,包括\n in 不能在純數字中使用,如果是字典的話判斷你的是字典的鍵 元組不能用 while 循環來顯示,因為內容是無序的 外層是集合,里面不能是列表 lst = [ [ "a" , 1 ] , { "b" , "250" }] dic = dict (lst) print (dic) 這里可能會輸出{ 'a' : 1 , 'b' : '250' }或者{ 'a' : 1 , '250' : 'b' } lst = [ "a1" , "b2" ] dic = dict (lst) print (dic) 這里會輸出{a: 1 ,b: 2 } lst = [ "a11" , "b22" ] dic = dict (lst) print (dic) 這里就會報錯 isinstance 比 type 用的多 在一個文件中 - 5 - 正無窮 a = 3 b = 3 a和b的 id 是一樣 a = b = 3 不管是不是 - 5 到正無窮,a和b的 id 都是一樣的 height = int (intput( "你身高多少:" ) ) 字符串為什么不能用 int 進行強轉(字符串是小數的) help ( print ) # print的幫助文檔 |
4. 小練習
問題:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
1. 利用 if 語句寫出猜大小的游戲: 設定一個理想數字比如: 66 , 讓用戶三次機會猜數字,如果比 66 大,則顯示猜測的結果大了; 如果比 66 小,則顯示猜測的結果小了; 只有等于 66 ,顯示猜測結果正確,退出循環。 最多三次都沒有猜測正確,退出循環,并顯示‘都沒猜對,繼續努力'。 2. 使用 while 和 for 遍歷字符串 "IG戰隊牛逼" 3. 使用 for 循環對s = "黃綠青藍紫" 進行循環,每次打印的內容是每個字符加上 "色的" , 例如:黃色的 綠色的 青色的 ... 4. 完成要求: 用戶可持續輸入( while 循環) 輸入A,則顯示走大路回家,然后在讓用戶進一步選擇: 是選擇公交車,還是步行? 選擇公交車,顯示 10 分鐘到家,并退出整個程序。 選擇步行,顯示 20 分鐘到家,并退出整個程序。 輸入B, 則顯示走小路回家,并退出整個程序。 輸入C, 則顯示繞道回家,然后在讓用戶進一步選擇: 是選擇游戲廳玩會,還是網吧? 選擇游戲廳,則顯示 ‘一個半小時到家,爸爸在家,拿棍等你。'并讓其重新輸入A,B,C選項。 選擇網吧,則顯示‘兩個小時到家,媽媽已做好了戰斗準備。'并讓其重新輸入A,B,C選項。 5. 寫代碼:計算 1 - 2 + 3 - 4 + ... + 99 中除了 88 以外所有數的總和? 6. (升級題)打印菱形小星星 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
答案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
""" 1.利用if語句寫出猜大小的游戲: 設定一個理想數字比如:66, 讓用戶三次機會猜數字,如果比66大,則顯示猜測的結果大了; 如果比66小,則顯示猜測的結果小了; 只有等于66,顯示猜測結果正確,退出循環。 最多三次都沒有猜測正確,退出循環,并顯示‘都沒猜對,繼續努力'。 """ """ times = 1 while times <= 3: num = input("請輸入一個數字:") if num.isdecimal(): num = int(num) if num > 66: print("結果大了") elif num == 66: print("對了 bingo老嬸") break elif num < 66: print("結果小了") else: print("抱歉,數字格式不對~") if times == 3: print("都沒猜對,繼續努力") times +=1 """ # 2.使用while和for 遍歷字符串 "IG戰隊牛逼" strvar = "IG戰隊牛逼" i = 0 while i < len (strvar): print (strvar[i]) i + = 1 for i in strvar: print (i) # 3.使用for循環對s="黃綠青藍紫"進行循環,每次打印的內容是每個字符加上"色的", # 例如:黃色的 綠色的 青色的 ... s = "黃綠青藍紫" for i in s: print (i + "色的" ) # 4.完成要求: # 用戶可持續輸入(while循環) # 輸入A,則顯示走大路回家,然后在讓用戶進一步選擇: # 是選擇公交車,還是步行? # 選擇公交車,顯示10分鐘到家,并退出整個程序。 # 選擇步行,顯示20分鐘到家,并退出整個程序。 # 輸入B, # 則顯示走小路回家,并退出整個程序。 # 輸入C, # 則顯示繞道回家,然后在讓用戶進一步選擇: # 是選擇游戲廳玩會,還是網吧? # 選擇游戲廳,則顯示 ‘一個半小時到家,爸爸在家,拿棍等你。'并讓其重新輸入A,B,C選項。 # 選擇網吧,則顯示‘兩個小時到家,媽媽已做好了戰斗準備。'并讓其重新輸入A,B,C選項。 """ sign = True while sign: opt = input("請輸入選項A,B,C") if opt.lower() == "a": print("走大路回家") opt = input("是選擇公交車,還是步行?") if opt == "公交車": print("10分鐘到家,") sign = False # break elif opt == "步行": print("20分鐘到家") sign = False # break elif opt.lower() == "b": print("走小路回家") break elif opt.lower() == "c": print("繞道回家") opt = input("是選擇游戲廳玩會,還是網吧?") if opt == "游戲廳": print("一個半小時到家,爸爸在家,拿棍等你。") elif opt == "網吧": print("兩個小時到家,媽媽已做好了戰斗準備。") """ # 5.寫代碼:計算 1 - 2 + 3 - 4 + ... + 99 中除了88以外所有數的總和? total = 0 for i in range ( 1 , 100 ): if i = = 88 : continue if i % 2 = = 1 : total + = i elif i % 2 = = 0 : total - = i print (total) total = 0 i = 1 while i < = 99 : if i = = 88 : i + = 1 continue if i % 2 = = 1 : total + = i elif i % 2 = = 0 : total - = i i + = 1 print (total) # 6.(升級題)打印菱形小星星 """ * *** ***** ******* ********* *********** *********** ********* ******* ***** *** * """ """ 空格 + 星星 + 換行 總行數: 對于任意個星星n ,總行數: n // 2 + 1 13 -> 7 11 -> 6 9 -> 5 7 -> 4 空格: 對于當前行i , 空格數量 = 總行數 - 當前行 1 => 5 2 => 4 3 => 3 4 => 2 5 => 1 6 => 0 星星: 對于當前行i , 星星數量 = 2 * 當前行 - 1 1 => 1 2 => 3 3 => 5 4 => 7 """ # n = int(input("輸入星星個數")) n = 13 hang = n / / 2 + 1 i = 1 while i < = hang: # 打印空格 kongge = hang - i print ( " " * kongge,end = "") # 打印星星 xingxing = 2 * i - 1 print ( "*" * xingxing,end = "") # 打印換行 print () i + = 1 i = hang while i > = 1 : # 打印空格 kongge = hang - i print ( " " * kongge,end = "") # 打印星星 xingxing = 2 * i - 1 print ( "*" * xingxing,end = "") # 打印換行 print () i - = 1 # 方法二 n = 11 hang = n / / 2 + 1 i = 1 while i < = hang: # 打印空格 kongge = hang - i while kongge> 0 : print ( " " ,end = "") kongge - = 1 # 打印星星 xingxing = 2 * i - 1 j = 1 while j < = xingxing: print ( "*" ,end = "") j + = 1 # 打印換行 print () i + = 1 i = hang while i > = 1 : # 打印空格 kongge = hang - i while kongge> 0 : print ( " " ,end = "") kongge - = 1 # 打印星星 xingxing = 2 * i - 1 j = 1 while j < = xingxing: print ( "*" ,end = "") j + = 1 # 打印換行 print () i - = 1 # (擴展了解) """abs 求絕對值的內置函數 abs(-1) = 1""" print ( "<===11111==>" ) for i in range ( - 6 , 7 ): # 只有一句代碼的話,可以直接寫在冒號的右邊; if i = = 0 : continue print ( " " * ( abs (i) - 1 ), "*" * ( 13 - 2 * abs (i))) |
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注服務器之家的更多內容!
原文鏈接:https://blog.csdn.net/weixin_46818279/article/details/120942405