为什么我的return语句会忽略python函数中的其余代码?
在我的函数中,我在return语句之后键入一个原始输入,然后继续调用我的函数。当我调用我的函数时,原始输入被完全忽略,只有return语句起作用为什么我的return语句会忽略python函数中的其余代码?,python,Python,在我的函数中,我在return语句之后键入一个原始输入,然后继续调用我的函数。当我调用我的函数时,原始输入被完全忽略,只有return语句起作用 def game(): #This selects 5 community cards from the pick_community function community = pick_community(5) card_4 = community[3] card_5 = co
def game():
#This selects 5 community cards from the pick_community function
community = pick_community(5)
card_4 = community[3]
card_5 = community[4]
first_3 = community[0:3]
return first_3
river = raw_input("If you are done with the round hit enter:" )
try:
if river =="":
return card_4
except:
print "Dont cheat man"
exit()
因为return语句不在函数中,所以代码的其余部分不会执行如果要返回前3个值,然后在代码中继续,可以使用
yield
执行。它基本上是将值插入生成器,然后最终返回整个生成器
更多信息请点击此处,或通过谷歌获取更多信息:):
return first_3
返回并因此结束函数。
剩下的代码将被忽略,因为您将永远无法通过返回。为什么要将
return
放在那里?您告诉函数return是因为return
就是这样做的。@keksnicoh:Oops!的确如此。:)为什么如果river==“”:返回卡4
在尝试中…除了?它永远不会引起例外。顺便说一句,除非你确切知道自己在做什么,否则你一般不应该使用未命名的,
。只是想知道,为什么这个问题被否决得如此糟糕?我的意思是,有人尝试编写代码,作者(可能是python新手)问为什么这样做不起作用。