I have a need for a code that asks for input of 5 pos numbers, then outputs these numbers backwards. I would like to use a while loop. This is what I've come up with so far, but it the second while loop doesn't work.
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. Basically modify second while loop to this.
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:
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. You are likely getting the following error.
TypeError: list indices must be integers or slices, not 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
# Works only once
while int(positiveNum) >= 0:
Now the next issue is that you are using positiveNum
as the index number. This will cause an IndexError
if the last number entered is greater than SIZE
, say 100.
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
# 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:
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:
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:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.