我尝试想出一种方法来执行反向字符串函数而不使用 [ : : -1] 方法。我是编码新手,我试图只使用“原始”步骤。以下是我的功能和规格。我想知道这是否是编写函数的有效方法。我很感激任何帮助。谢谢!
def reverse(word):
x = -2 #word in reversed order counter
y = 1 #starts counter to end "while" statement below
reversed = word[-1] #starts the reversed-word at last letter of word
while len(word) > y: #ending parameter for when all letters run through loop
reversed += word[x] #adds letters to reversed word starting at word[-2]
x -= 1 #moves position in word 1 to the left
y += 1 #increases the counter by 1
return reversed