[英]Returning None on Python
我的學生正在上課模擬岩石,紙張,剪刀。 一組有一個錯誤,其中一個函數不會返回任何東西。 我檢查了一下,看看是否所有分支都具有return語句,並且確實如此。 我也嘗試過使用可視化工具,但功能只是停止,我也不知道為什么。
def Win_Loss(my_history, their_history):
if len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 'r'):
return 'p'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'r' and my_history[-1] == 'p'):
return 's'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'p' and my_history[-1] == 's'):
return 'r'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 'p'):
return 'r'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'r' and my_history[-1] == 's'):
return 'p'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'p' and my_history[-1] == 'r'):
return 's'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 's'):
return 'r'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'r' and my_history[-1] == 'r'):
return 'p'
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'p' and my_history[-1] == 'p'):
return 's'
else:
return "p"
print(Win_Loss(['s','p','p'],['s','p','p']))
這應該是打印“ s”,但它打印“無”。
如果任何內部if
失敗,它將返回None
,因為采用了外部if / elif,因此將不會執行else:
塊。
要明確的是,它們的整個功能等效於:
def Win_Loss(my_history, their_history):
if len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 'r'):
return 'p'
else:
return "p"
因為一旦采取了第一個if
,就不能采用所有其他elif
。 由於它們具有相同的條件,因此將永遠無法達到所有elif
。
如果您將else:
取出,並且僅默認return "p"
,它將保證返回值:
elif len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 'p' and my_history[-1] == 'p'):
return 's'
# If anything else happens
return "p"
但這不能解決根本問題。
另外,他們可以結合條件:
if len(my_history) > 1 and len(their_history) > 1 and
(their_history[-1] == 's' and my_history[-1] == 'r'):
return 'p'
這也將避免出現此問題,因為沒有內部條件可以跳過收益,而else:
將按預期工作。
正如其他人提到的那樣,整個外部if塊是多余的。 對於我的女孩(我教11-13歲),建議他們將默認返回值移到頂部...然后,您不必檢查每個列表是否足夠大:
if not (len(my_history) > 1 and len(their_history) > 1):
return "p" # default value
elif their_history[-1] == 's' and my_history[-1] == 'r':
return 'p'
elif their_history[-1] == 'r' and my_history[-1] == 'p':
return 's'
elif their_history[-1] == 'p' and my_history[-1] == 's':
return 'r'
elif their_history[-1] == 's' and my_history[-1] == 'p':
return 'r'
elif their_history[-1] == 'r' and my_history[-1] == 's':
return 'p'
elif their_history[-1] == 'p' and my_history[-1] == 'r':
return 's'
elif their_history[-1] == 's' and my_history[-1] == 's':
return 'r'
elif their_history[-1] == 'r' and my_history[-1] == 'r':
return 'p'
elif their_history[-1] == 'p' and my_history[-1] == 'p':
return 's'
如果出現問題,請在末尾添加一個例外:
raise AssertionError, "a combination not covered was encountered"
然后,這將確保我們知道是否忘記了可能性。
取決於其能力,可能不應該討論的其他樣式點:
通過嵌套其條件,它們可以減少重復,盡管如果要調整其機器人,這是一個障礙,而不是收益。
if not len(my_history) > 1 and len(their_history) > 1:
return "p" # default value
elif their_history[-1] == 's': # if they threw scissors
if my_history[-1] == 'r': # and I threw rock
return 'p' # throw paper
return 'r' # otherwise throw rock
elif their_history[-1] == 'r':
if my_history[-1] == 'p':
return 's'
return 'p'
elif their_history[-1] == 'p':
if my_history[-1] == 's':
return 'r'
return 's'
您的elif
語句處於錯誤的級別, 如@SiongThyeGoh所述 。 盡管效果不佳,但以下內容仍然可以正常工作。
def Win_Loss(my_history, their_history):
if len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 'r'):
return 'p'
elif (their_history[-1] == 'r' and my_history[-1] == 'p'):
return 's'
...
但這將是我最喜歡的解決方案:
def Win_Loss(my_history, their_history):
d = {frozenset({'s', 'r'}): 'p',
frozenset({'r', 'p'}): 's',
frozenset({'s', 'p'}): 'r',
frozenset({'s', 's'}): 'r',
frozenset({'r', 'r'}): 'p',
frozenset({'p', 'p'}): 's'}
if len(my_history) > 1 and len(their_history) > 1:
return d.get(frozenset({their_history[-1], my_history[-1]}))
else:
return "p"
print(Win_Loss(['s','p','p'],['s','p','p']))
if len(my_history) > 1 and len(their_history) > 1:
if (their_history[-1] == 's' and my_history[-1] == 'r'):
return 'p'
查看前三行,如果滿足條件的第一行但不滿足第二條,則您不返回任何內容,即得到None。
同樣,如果此條件通過:
if len(my_history) > 1 and len(their_history) > 1:
那么它將進入前三行,否則,其他elif語句將被忽略,因為它是相同條件,並且只會到達else部分並返回p。
它只能返回p或無。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.