擅长:python、mysql、java
<p>您混淆了<strong>打印</strong>和<strong>返回</strong>。<strong>打印</strong>将值转储到输出(您的屏幕),但不会更改内存中的任何数据,也不会对该值做任何进一步的操作。<strong>返回</strong>将其作为函数值返回。你知道吗</p>
<p>正如编译器警告告诉我的那样,没有一个函数向调用者返回任何值。因此,您得到“None”作为值,并且您的调用程序会出错。用<strong>return</strong>替换所有的terminal<strong>print</strong>语句,代码运行良好:</p>
<pre><code>def first_word(string):
first_space_pos = string.find(" ")
word = string[0:first_space_pos]
return word
def last_words(string):
first_space_pos = string.find(" ")
words = string[first_space_pos+1:]
return words
def reverse(string):
words = string.count(" ") +1
count = 1
string_reversed = ""
while count < words:
string_reversed = first_word(string) + str(" ") + string_reversed
string = last_words(string)
count += 1
return string_reversed
print reverse("Now is the time for all good parties to come to the aid of man.")
</code></pre>
<p>输出:</p>
<pre><code>of aid the to come to parties good all for time the is Now
</code></pre>