擅长:python、mysql、java
<p>您在<code>len(a)-1</code>中删除了<code>-1</code>,在<code>and</code>中更改了顺序(因此,当<code>x == len(a)</code>时,它不会试图获得<code>a[x]</code>,因为<code>"index out of range"</code>)</p>
<pre><code>while (x <= len(a)):
if (x != len(a)) and (a[x] != " "):
</code></pre>
<p>对我有用的完整版本</p>
<pre><code>import os
import string
a = "This is an example"
temp = []
store = []
print(a)
x = 0
while (x <= len(a)):
if (x != len(a)) and (a[x] != " "):
temp.append(a[x])
x += 1
else:
temp.reverse()
store.extend(temp)
store.append(' ')
del temp[:]
x += 1
str1 = ''.join(store)
print(str1)
</code></pre>