【发布时间】:2015-08-07 20:56:44
【问题描述】:
# word reverser
#user input word is printed backwards
word = input("please type a word")
#letters are to be added to "reverse" creating a new string each time
reverse = ""
#the index of the letter of the final letter of "word" the users' input
#use this to "steal" a letter each time
#index is the length of the word - 1 to give a valid index number
index = len(word) - 1
#steals a letter until word is empty, adding each letter to "reverse" each time (in reverse)
while word:
reverse += word[index]
word = word[:index]
print(reverse)
print(reverse)
input("press enter to exit")
致力于制作一个简单的程序,将用户输入的单词反向拼写并通过“窃取”原始字母并从中创建新字符串来将其打印回给用户。 我遇到的麻烦是这段代码在 反向 += 单词[索引] 获得相同结果的帮助或更好的方法非常感谢。
【问题讨论】:
-
在
reverse +=...行之前放置一个print(index, len(word))行,看看会发生什么。
标签: python string indexing range