用while和数组重复输入

时间:2018-09-13 02:56:42

标签: arrays character multiple-instances repeat

我正在尝试编写一个代码,其中他要求单词的长度(n,n <= 100000),然后是单词本身,直到n等于零并结束处理;输出要求将所有单词都反转。

Entrance         Output  

     7           odacova        
   avocado        esuoh
     5
   house
     0

这是我正在尝试执行的代码:

numeros = []
word = []
for i in numeros:
   while i <= 100000:
      n = int(input())  #always ask the length and the word
      s = str(input())
      numeros.append(n)
      word.append(s)
      if i == 0:
         for w in word:
            print(w[::-1])   #output with the words reversed
            break

1 个答案:

答案 0 :(得分:0)

这是因为数组numeros开头是空的,因此您的for循环甚至不会运行。我还严重依赖于您对所需内容的描述,因为您的代码与您的描述完全不同。这应该可行:

numeros = []
words = []

while True:
    n = int(input())    # get number of characters input
    if n == 0:    # if input is 0, print all reversed words and exit the program
        for w in words:
            print(w[::-1])   #output with the words reversed
        break
    if n <= 100000:
        s = str(input())    # get word input
        numeros.append(n)
        words.append(s)