简体   繁体   English

输出向后输入数字的Python列表

[英]Python list that outputs numbers inputted backwards

I have a need for a code that asks for input of 5 pos numbers, then outputs these numbers backwards. 我需要一个代码,要求输入5个pos数字,然后向后输出这些数字。 I would like to use a while loop. 我想使用while循环。 This is what I've come up with so far, but it the second while loop doesn't work. 到目前为止,这是我想出的,但是第二个while循环不起作用。

positiveNum = 0
SIZE = 5
numberList= []
ARRAY_LIMIT = SIZE -1

while len(numberList) < SIZE :
    positiveNum = input("Enter a positive number:")
    numberList.append(positiveNum)
while int(positiveNum) >= 0:
    print(numberList[positiveNum])
    positiveNum -= 1

You should be iterating on length of numberList and not positive num. 您应该迭代numberList的长度,而不是正数。 Basically modify second while loop to this. 基本上修改第二个while循环对此。

 i = SIZE; 
 while i>0: 
        print(numberList[i-1])
        i=i-1

On the second loop, you are using the same positiveNum variable without reseting it to the size of the array, try: 在第二个循环中,您正在使用相同的positiveNum变量,而没有将其重置为数组的大小,请尝试:

SIZE = 5
numberList= []
ARRAY_LIMIT = SIZE -1

while len(numberList) < SIZE :
    positiveNum = input("Enter a positive number:")
    numberList.append(positiveNum)
index = SIZE - 1
while index >= 0:
    print(numberList[index])
    index -= 1

Your first issue is that input returns a string so you need to cast it to an int if you want to index with it. 您的第一个问题是input返回一个string因此如果要使用它进行索引,则需要将其强制转换为int You are likely getting the following error. 您可能会收到以下错误。

TypeError: list indices must be integers or slices, not str TypeError:列表索引必须是整数或切片,而不是str

# Won't work with string
numberList[positiveNum]
positiveNum -= 1
# Need to cast to int first
positiveNum = int(input("Enter a positive number:"))

Converting it in your while loop condition will only work for the condition, it does not change that value in the variable to an int , it is still a string 在while循环条件中转换它仅适用于该条件,它不会将变量中的值更改为int ,它仍然是string

# Works only once
while int(positiveNum) >= 0:

Now the next issue is that you are using positiveNum as the index number. 现在,下一个问题是您正在使用positiveNum作为索引号。 This will cause an IndexError if the last number entered is greater than SIZE , say 100. 如果最后输入的数字大于SIZE (例如100),这将导致IndexError

SIZE = 5
number_lst = []

while len(number_lst) < SIZE:
    # Should perform error checking if you must have positive numbers
    num = int(input("Enter a positive number: "))
    number_lst.append(num)

# Output backwards using while
i = len(number_lst) - 1
while i >= 0:
    print(number_lst[i])
    i -= 1

Here is also a couple of for loop alternatives 这也是for循环的几种选择

# Output backwards using for
for item in number_lst[::-1]:
    print(item)

for item in reversed(number_lst):
    print(item)

for i in range(len(number_lst) - 1, -1):
    print(number_lst[i])

for i in reversed(range(len(number_lst))):
    print(number_lst[i])

Using while loops: 使用while循环:

size = 5
number_list = []

while len(number_list) < size:
    number_list.append(int(input("Enter a positive number: ")))
i = 1
while i <= size:
    print(number_list[size - i])
    i += 1

Using a for loop for the second loop: 在第二个循环中使用for循环:

size = 5
number_list = []

while len(number_list) < size:
    number_list.append(int(input("Enter a positive number: ")))
for i in number_list[::-1]:
    print(i)

Using two for loops, which would be more sensible in this case: 使用两个for循环,在这种情况下会更明智:

size = 5
number_list = []

for _ in range(size):
    number_list.append(int(input("Enter a positive number: ")))
for i in number_list[::-1]:
    print(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM