[英]what is a more elegant way of returning True and False with this if/else statement in Python?
我不斷收到錯誤消息,說“返回”后出現不必要的“其他”
它試圖告訴我什么,以及對這個邏輯進行編碼的更優雅的方式是什么?
for selected_node in NODES:
if pm.nodeType(selected_node) == 'file':
msg = 'correct type of nodes selected'
LOGGER_TEXTURE_SWAP.debug(msg)
return True
else:
msg = 'incorrect type of nodes selected'
LOGGER_TEXTURE_SWAP.debug(msg)
return False
我嘗試重新創建您的代碼片段的邏輯,並使用最新的 python 3.8.3
nodes = ['file', 'folder', 'directory']
for node in nodes:
if node == 'file':
print(node)
return True
else:
print('something else')
return False
我得到 SyntaxError: 'return' 在 function 之外。 這是可以理解的,因為返回應該是 function 的 output。
所以,然后我把它做成function,如下。
nodes = ['file', 'folder', 'directory']
def myprogram():
for node in nodes:
if node == 'file':
print(node)
return True
else:
print('something else')
return False
myprogram()
現在我打印了“文件”和“真”output。 因此,python 按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.