-6
在python中,它帶出'返回函數外部',我檢查縮進沒有錯。任何線索?「返回功能錯誤」錯誤與正確的縮進?
dict={1:10,2:20,3:30}
for a,b in dict.items():
if b==30:
return a
在python中,它帶出'返回函數外部',我檢查縮進沒有錯。任何線索?「返回功能錯誤」錯誤與正確的縮進?
dict={1:10,2:20,3:30}
for a,b in dict.items():
if b==30:
return a
沒有功能,所以你不能使用返回。您可能會包裹代碼def
:
d={1:10,2:20,3:30}
def return_30(d):
for a,b in d.items():
if b==30:
return a
而且我改名dict
到d
,因爲dict
是該類型的名稱,當你重新定義它,你無法訪問原始dict
。
for
循環,不是函數。
def function():
print(1+2) # Im inside the function, return keyword here is valid
# I'm outside the function.
for x in range(10):
print(x)
#This is not a function, return keyword here is invalid.
哪兒功能: Python中的函數使用
def
關鍵字中定義? –