導航:首頁 > 編程語言 > 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相關的資料

熱點內容
我的世界本地伺服器默認地址 瀏覽:131
我的世設置命令方塊會閃退 瀏覽:968
加密媒體怎麼輸密碼 瀏覽:161
方舟如何刪除伺服器數據 瀏覽:600
用閱讀app怎麼看18 瀏覽:689
gn文件編譯 瀏覽:783
酷閃加密系統 瀏覽:820
區塊鏈數據加密 瀏覽:389
3d命令集 瀏覽:289
單片機的智能產品工廠里有 瀏覽:300
事業單位程序員有必要去嗎 瀏覽:583
人工智慧訓練小鳥python 瀏覽:132
怎樣把兩個pdf合並成一個 瀏覽:681
什麼app帶仿製圖章 瀏覽:420
單片機CJNE什麼意思 瀏覽:569
廊坊伺服器是什麼 瀏覽:763
客戶解壓要打欠條還是收據 瀏覽:774
通過app組件啟動有什麼壞處 瀏覽:777
不屬於國產密碼演算法 瀏覽:863
單片機LED顯示字型檔 瀏覽:355