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

熱點內容
安卓打游戲都是用什麼錄屏 瀏覽:930
107區的伺服器是什麼 瀏覽:658
非對稱加密的加密簽名的過程 瀏覽:443
mysqlinsert命令 瀏覽:198
電腦盤加密碼打開後怎麼鎖起來 瀏覽:174
安卓系統是什麼代碼編譯的 瀏覽:295
解壓單車模擬器游戲 瀏覽:501
應用程序員需要懂很多硬體知識嗎 瀏覽:396
我的世界伺服器110地址大全 瀏覽:624
怎麼qq相冊加密自己也不能看 瀏覽:22
linuxc語言串口數據 瀏覽:857
mac下編寫python 瀏覽:973
厚襯衣程序員 瀏覽:743
一年級編程精彩內容 瀏覽:578
cc2540編程 瀏覽:794
越南離北京源碼 瀏覽:639
服裝展示網站源碼 瀏覽:325
編譯器過度優化線 瀏覽:689
安卓怎麼邊瀏覽邊錄視頻 瀏覽:653
分支限界java 瀏覽:389