擅长:python、mysql、java
<p>您的<code>direction</code>函数不<code>return</code>任何内容,因此它默认为<code>None</code>。这就是为什么当您打印函数时,它返回<code>None</code>。您可以使用<code>yield</code>:</p>
<pre><code>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]
</code></pre>
<p>然后将其运行为:</p>
^{pr2}$
<p><code>direction</code>函数现在返回一个<a href="http://docs.python.org/2/tutorial/classes.html#generators" rel="nofollow">^{<cd7>}</a>,您可以遍历并打印这些值。在</p>
<hr/>
<p>或者,您可以简单地不使用<code>print</code>:</p>
<pre><code>>>> direction('forward')
w
wo
wor
word
</code></pre>