def direction(x):
"""Type direction of word to be spelled as str, forward or reverse."""
if x == 'reverse':
for x in range(wordlength, 0, -1):
yield word[:x]
elif x == 'forward': # Also, I changed the "if" here to "elif" (else if)
for x in range(0, wordlength + 1):
yield word[:x]
只需执行}本身。尝试执行
direction('forward')
而不是print direction('forward')
。direction
已经处理了{print direction('forward')
只需执行direction('forward')
(打印出w
,wo
,等等),然后打印出direction('forward')
的返回值,None
,因为它没有返回任何内容,也没有理由返回任何内容。在您的
direction
函数不return
任何内容,因此它默认为None
。这就是为什么当您打印函数时,它返回None
。您可以使用yield
:然后将其运行为:
^{pr2}$direction
函数现在返回一个^{或者,您可以简单地不使用
print
:相关问题 更多 >
编程相关推荐