【发布时间】:2016-04-25 15:16:05
【问题描述】:
对于一个作业,我需要使用while 循环来反转一个列表,我就是做不到。
这是我必须帮助我开始的示例代码:
sentence = raw_int (" ")
length = len(sentence) # determines the length of the sentence (how many characters there are)
index = length - 1 #subtracts one from the length because we will be using indexes which start at zero rather than 1 like len
while... #while the index is greater than or equal to zero continue the loop
letter = sentence[index] #take the number from the index in the sentence and assigns it to the variable letter
我需要在我的解决方案中使用它。
【问题讨论】:
-
我不懂 Python,但很明显,您的 While 循环将查看变量“index”(因此,“while index > 0”)。在 while 循环中,您可能会从 index 中减去 1,变量“letter”将是您打印的字符。
标签: python while-loop reverse