<p>我试着写一个程序,从输入的字母中构造新词,反之亦然</p>
<pre><code># vise versa
print ("word vise versa")
word = input("Input your text ")
new_word = ""
while word:
position = len(word) - 1
for letter in word:
new_word += letter[position]
position -= 1
print(new_word)
</code></pre>
<p>总是有错误</p>
<pre><code> Traceback (most recent call last):
File "4_2.py", line 9, in <module>
new_word += letter[position]
IndexError: string index out of range
</code></pre>
<p>我做错了什么?
谢谢!你知道吗</p>
<p>问题可能是你在下面几行所做的</p>
<pre><code>for letter in word:
new_word += letter[position]
</code></pre>
<p>其中字母是单词中的每个字母,如果单词是<code>abc</code>,则首先是“a”,然后是“b”,然后是“c”。在seconds字符串上,您试图使用字母“a”作为数组,这是不好的。您可能希望偏移到单词数组中?你知道吗</p>