Cannot loop within function

I’m trying to write a function that loops through and checks if a number is divisible by 10. I cannot figure out what to put in the return statement to ensure that the loop continues to run through until completion.

Here’s the question:

Create a function named divisible_by_ten() that takes a list of numbers named nums as a parameter. Return the count of how many numbers in the list are divisible by 10 .

Here is my code:

#Write your function here

def divisible_by_ten(nums):
  i = 0
  for i in nums:
    if i % 10 == 0:
      return nums[i]

#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))

Any ideas?

Check the placement of return. Should it be inside the loop?

Add to that, what is the return expected to be?

1 Like

@mtf

If I put the return outside of the loop I get an indentation error so my thought is that it should be contained within the loop.

I need to return how many numbers are divisible by 3 within the list, so I guess I could do something like this:

#Write your function here
i = 0
def divisible_by_ten(nums):
  for i in range(len(nums)):
    if i % 10 == 0:
      return nums.count(i)    
  
#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))

It’s returning a value of 1 so I know the code works but how do I get it to loop through the rest of the numbers in the list?

That would mean completing the iteration, right?

for ...

return ...

@mtf

My fault I meant to say how many numbers are divisible by 10.

That would mean completing the iteration, right?

Yes at some point I need to complete the iteration but no matter where I put the return I get a indentation error. I think the iteration should run to completion based on the loop with for and return.

#Write your function here
i = 0
def divisible_by_ten(nums):
  for i in range(len(nums)):
    if i % 10 == 0:
      new_num = nums[i] + 1
      return nums.count(i)
  
#Uncomment the line below when your function is done
print(divisible_by_ten([20, 25, 30, 35, 40]))

Try copying the indentation on the for line and paste it on the return line. The two keywords should line up, as shown earlier.

What should be happening inside the loop? How are you accumulating your result from the loop? How does that fit into the return value?

Try copying the indentation on the for line and paste it on the return line. The two keywords should line up, as shown earlier.

Got it. I did not know that. Indentation can be confusing in python.

What should be happening inside the loop? How are you accumulating your result from the loop? How does that fit into the return value?

Inside the loop it should iterate through each number in the list. I am accumulating it in my return statement and it is being stored within a variable. It fits into the return value by returning the number of times a number is divisible by 10.

i = 0
def divisible_by_ten(nums):
  for i in range(len(nums)):
    if i % 10 == 0:
  return nums.count(i)
  • i starts out at 0 to ensure that the value is reset before the function.

  • The function is then defined as divisible_by_ten with one parameter nums.

  • Inside the function we begin with a for loop by using the placeholder i, that goes through the length/range of the nums variable/list.

  • if i finds a number that is divisible by 10 then we return it as part of the count.

  • the loop continues until the last number in the list and completes.

What does the program print?

What happens in the if statement?

What is the value of i in the return statement?

It realizes that the first number is divisible by 10 therefore returns the count of 1 from the call in the print statement. What I don’t understand, is how I can get it to check the second number in the list.

for i in range(len(nums)):
    print (i)

Given there are five items in the list, we should see,

0
1
2
3
4

Where does i figure into your program? As an index, yes, but is it that value we wish to test for even parity?

4 Likes

@mtf

I see so I need to somehow pull the values out of the list. Right now I am imply gathering each value’s location within the index.

#Write your function here
i = 0
def divisible_by_ten(nums):
  for i in range(len(nums)):
    if i % 10 == 0:
  return nums[i]

I just don’t understand how this would be returning an indentation error. I copied the space before the for and I’m still getting the following error:

 File "script.py", line 6
    return nums[i]
    ^
IndentationError: expected an indented block

This is a parity test on the index. Should it not be the value at that index we are testing?

I’m not sure what a parity test is but I think I see what you are saying:

    if nums[i] % 10 == 0:

Now this checks the value contained within the index.

D’oh!. Division by 2 would be a parity test. Division by 10 is a factor test. We want to determine if 10 is a factor of N, where N is the number we’re testing.

You’ve caught on, though. Now you are testing the value for divisibility, rather than the index.

Do we need the index if we can iterate the values directly? Think of.

1 Like

Do we need the index if we can iterate the values directly? Think of .

Probably not. I’ve written this and it now prints out the numbers in the list that are divisible by 10.

def divisible_by_ten(nums):
  for i in range(len(nums)):
    if nums[i] % 10 == 0:
     print(nums[i])
1 Like

@mtf

def divisible_by_ten(nums):
  for i in range(len(nums)):
    if nums[i] % 10 == 0:
  return print(nums[i])

Would you know why I am getting an indentation error here with the return statement? We went over this yesterday, and I am doing nothing different. I feel like there is an issue with codecademy’s compiler.

Because there is no code block attached to the if statement. return is not part of that logic.

Again, what are we expecting the function to return?

We want it to return the count of numbers in the list that are divisible by ten.

The if statement checks to see if that’s true, if it is then it returns that number if it’s not it moves on to the next number.

One wonders what your meaning of return is. What do you take it to mean?