導航:首頁 > 編程語言 > python函數中的return

python函數中的return

發布時間:2024-09-25 14:08:08

python怎麼return後讓循環繼續運行

return 會直接另函數返回,函數就運行結束了,所有該函數體內的代碼都不再執行了,所以該函數體內的循環也不可能再繼續運行。

如果你需要讓循環繼續執行,就不能return函數,而應該選用break或者continue。
break:跳出所在的當前整個循環,到外層代碼繼續執行。
continue:跳出本次循環,從下一個迭代繼續運行循環,內層循環執行完畢,外層代碼繼續運行。
return:直接返回函數,所有該函數體內的代碼(包括循環體)都不會再執行。

用下邊的示例代碼來解釋:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

def return_continue_break(type):
if(not type in ["return", "continue", "break"]):
print '"type" should be "return, continue, break".'
return
for j in range(0, 10):
for i in range(0, 10):
print "j_i: %d_%d" %(j, i)
if(i > 3):
if(type == "return"):
return
elif(type == "continue"):
continue
else:
break
print "executed!"

if __name__ == '__main__':
return_continue_break("break")
return_continue_break("continue")
return_continue_break("return")

BREAK的輸出為:

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

j_i: 0_0
executed!
j_i: 0_1
executed!
j_i: 0_2
executed!
j_i: 0_3
executed!
j_i: 0_4
j_i: 1_0
executed!
j_i: 1_1
executed!
j_i: 1_2
executed!
j_i: 1_3
executed!
j_i: 1_4
j_i: 2_0
executed!
j_i: 2_1
executed!
j_i: 2_2
executed!
j_i: 2_3
executed!
j_i: 2_4
j_i: 3_0
executed!
j_i: 3_1
executed!
j_i: 3_2
executed!
j_i: 3_3
executed!
j_i: 3_4
j_i: 4_0
executed!
j_i: 4_1
executed!
j_i: 4_2
executed!
j_i: 4_3
executed!
j_i: 4_4
j_i: 5_0
executed!
j_i: 5_1
executed!
j_i: 5_2
executed!
j_i: 5_3
executed!
j_i: 5_4
j_i: 6_0
executed!
j_i: 6_1
executed!
j_i: 6_2
executed!
j_i: 6_3
executed!
j_i: 6_4
j_i: 7_0
executed!
j_i: 7_1
executed!
j_i: 7_2
executed!
j_i: 7_3
executed!
j_i: 7_4
j_i: 8_0
executed!
j_i: 8_1
executed!
j_i: 8_2
executed!
j_i: 8_3
executed!
j_i: 8_4
j_i: 9_0
executed!
j_i: 9_1
executed!
j_i: 9_2
executed!
j_i: 9_3
executed!
j_i: 9_4

RETURN的輸出為:

1
2
3
4
5
6
7
8
9

j_i: 0_0
executed!
j_i: 0_1
executed!
j_i: 0_2
executed!
j_i: 0_3
executed!
j_i: 0_4

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
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

j_i: 0_0
executed!
j_i: 0_1
executed!
j_i: 0_2
executed!
j_i: 0_3
executed!
j_i: 0_4
j_i: 0_5
j_i: 0_6
j_i: 0_7
j_i: 0_8
j_i: 0_9
j_i: 1_0
executed!
j_i: 1_1
executed!
j_i: 1_2
executed!
j_i: 1_3
executed!
j_i: 1_4
j_i: 1_5
j_i: 1_6
j_i: 1_7
j_i: 1_8
j_i: 1_9
j_i: 2_0
executed!
j_i: 2_1
executed!
j_i: 2_2
executed!
j_i: 2_3
executed!
j_i: 2_4
j_i: 2_5
j_i: 2_6
j_i: 2_7
j_i: 2_8
j_i: 2_9
j_i: 3_0
executed!
j_i: 3_1
executed!
j_i: 3_2
executed!
j_i: 3_3
executed!
j_i: 3_4
j_i: 3_5
j_i: 3_6
j_i: 3_7
j_i: 3_8
j_i: 3_9
j_i: 4_0
executed!
j_i: 4_1
executed!
j_i: 4_2
executed!
j_i: 4_3
executed!
j_i: 4_4
j_i: 4_5
j_i: 4_6
j_i: 4_7
j_i: 4_8
j_i: 4_9
j_i: 5_0
executed!
j_i: 5_1
executed!
j_i: 5_2
executed!
j_i: 5_3
executed!
j_i: 5_4
j_i: 5_5
j_i: 5_6
j_i: 5_7
j_i: 5_8
j_i: 5_9
j_i: 6_0
executed!
j_i: 6_1
executed!
j_i: 6_2
executed!
j_i: 6_3
executed!
j_i: 6_4
j_i: 6_5
j_i: 6_6
j_i: 6_7
j_i: 6_8
j_i: 6_9
j_i: 7_0
executed!
j_i: 7_1
executed!
j_i: 7_2
executed!
j_i: 7_3
executed!
j_i: 7_4
j_i: 7_5
j_i: 7_6
j_i: 7_7
j_i: 7_8
j_i: 7_9
j_i: 8_0
executed!
j_i: 8_1
executed!
j_i: 8_2
executed!
j_i: 8_3
executed!
j_i: 8_4
j_i: 8_5
j_i: 8_6
j_i: 8_7
j_i: 8_8
j_i: 8_9
j_i: 9_0
executed!
j_i: 9_1
executed!
j_i: 9_2
executed!
j_i: 9_3
executed!
j_i: 9_4
j_i: 9_5
j_i: 9_6
j_i: 9_7
j_i: 9_8
j_i: 9_9

Ⅱ python中return函數的用法

在 Python 中,「return」函數的用法如下:

1、return 語句用於退出函數,終止函數並將 return 值傳回。實例:

>>>defa(x,y):
>>>ifx==y:
>>>returnx,y

2、用於同一循環語句下,遇到第一個 return 後即返回。實例:

```python
>>>deffun(a,b):
print(a)
returna
print(b)
returnb

>>>resunlt=fun(2,6)
2
```

3、用於 return 在不帶參數的情況下(或者沒有寫 return 語句),默認返回 None。實例:

defrecurve(a,b):
ifa%b==0:
returnb
else:
gcd(b,a%b)

以上內容參考:網路-Python

閱讀全文

與python函數中的return相關的資料

熱點內容
python入門迷宮 瀏覽:67
Python打折代碼不含商品 瀏覽:218
把多個Word合成一個pdf 瀏覽:354
aes演算法描述 瀏覽:899
新手機壓縮包在哪 瀏覽:779
java抽獎程序源碼 瀏覽:700
汽車壓縮機又叫 瀏覽:95
android讀取data文件 瀏覽:874
紅旗智聯app怎麼跟h5車子連接 瀏覽:138
材料化學pdf 瀏覽:114
伺服器機房都有什麼東西 瀏覽:370
最近長陰短柱量能副圖指標源碼 瀏覽:647
python字元串去除後四位 瀏覽:167
捷速pdf編輯器破解版 瀏覽:725
大帶寬伺服器怎麼租 瀏覽:299
籃球程序員單身難嗎 瀏覽:877
一接到命令就 瀏覽:488
挖幣伺服器是什麼 瀏覽:524
攜帶型u盤加密 瀏覽:464
apex找不到伺服器怎麼解決 瀏覽:316