Home > other >  How to iterate through a list using index
How to iterate through a list using index

Time:04-06

I am making a function that counts from 0 to a certain number input I tried to make it using print and that worked but the output needs to be a return value so I tried making a list then iterating through it using a for loop and returning each value of the list back using the index of the list but that didn't work it only returns the last value of the list I also want the returned value to be a string, not a list.

this is my code

nums = []
def numbers_range(number):
  for i in range(number 1):
    numz = nums.append(i)
  for g in range(len(nums)):
   numb = nums[g]
  return numb

print(numbers_range(5))

result 5 the result i want 0 1 2 3 4 5 please help

CodePudding user response:

Your for loop containing variable 'g' is putting values 1 over next in same variable numb. Basically each value in numb is being overridden by new value from list.

Do this instead.

def numbers_range(number):
  nums = []
  for i in range(number 1):
    nums.append(i)
  
  ans = "".join([str(no) for no in nums])
  return ans

print(numbers_range(5))

Here in code,

  1. We are creating a new list called nums.

  2. Then we are traversing from 0 to the number and appending each value into the list.

  3. Finally returning the list converted to string.

CodePudding user response:

for i in range(number 1):
    numz = nums.append(i)

for g in range(len(nums)):
    numb = nums[g]

What you're doing here is iterating through a list and assign the items to variable one by one. In each turn the variable is overwritten. So when the loop terminates only the value assigned is final value in the list.

In case you need to return a sequence of numbers you need to do is implement your code with eval function. But the returning values are strings, not integers. As you're just returning them, it won't be an issue.

def numbers_range(number):
    x =0
    s =''
    while x<=n:
# if you don't need a new line replace '\n' with a space ' ' .
         s =(s str(x) "\n")
         x =x 1
    numb = eval("print(s)")
    return numb

when using the function,

numbers_range(5)

output

0
1
2
3
4
5

Hope this answer your question.

CodePudding user response:

one solution is

def numbers_range(number):
  for i in range(number 1):
    yield i;


result = numbers_range(5)
for i in result:
    print(i,end=" ")

another solution is

def numbers_range(number):
    ans = []
    for i in range(number 1):
        ans.append(i)
    return ans
    


result = numbers_range(5)
for i in result:
    print(i,end=" ")

CodePudding user response:

So I think the issue is in your use of range(). In my experience, range() needs to be formatted like range(min, max) whereas you are formatting it like range(min).

I think what you want would come from this code:

nums = []
def numbers_range(number):
  for i in range(0, number 1):
    numz = nums.append(i)
  for g in range(0, len(nums)):
   numb = nums[g]
  return numb

print(numbers_range(5))

Hope this works for you!

  • Related