Why it is not returning as a list?

hi , i have come up with this code with my current knowledge.
Why it is not returning as a list?

There is return inside of your for loop.
As I am understanding, start == 91.
So, your first iteration will have “i = 91”.
Then, it will add start + 3 which is equal to 94.
Immediately right after, the program will return the iist and the program will end.

Remember, the return in a function always ends the function.

You haven’t told it what to do if results are not returned inside the loop. There is no return outside of it.

In other cases that would be correct. However, in this instance, since the for loop will always be engaged, the return in the for loop will always be engaged on the first cycle of the loop and end the program quickly

The other note is that your program is returning list. However, it is only return one element in a list.
There will be other bugs when you remove the return. See if you can fix the problem that you find in the for loop after the return removal.

if i remove return statements,
it prints out “None”

There’s a few issues here. Quick list:
You have a for loop that will always try to loop from 0 to 100 regardless of input.
There’s a return statement inside the for loop which will inevitably exit on the first iteration of this loop.
The logic to return lst1 in the event that start > 100 might not be in the ideal location.
If you removed all return statements from your function the function defaults to returning None.

Check your logic and statements you’re using to perform each step to make sure it works the way you expect it to. Take your time and try to fix everything. If you’re still stuck after trying for a while please fire the code you’ve changed and what you expect to happen back to the forums. If you can please format your code too which makes it easy to debug, ta. Details on formatting are here-

1 Like

Dear Frantz,
I cannot fix the bug, can you help me?

Bugfixing… most problem solving, is about making observations, identifying problems.

So pick a problem.
Describe what it’s doing.
Describe what it should do instead.
Describe the difference between those.
Describe who’s (what part of your code) responsible for making that so.
Go to that location in the code, look at the inputs it receives (the starting state) and the output.
Is the input correct?
Is the output wrong?
If so, then you’ve found the location where the problem is and can reconsider what should happen there.

Most of those steps are pretty easy. So there’s plenty you can do beyond just “cannot fix it”.
It may be that there’s something that you cannot do, but that’s just part of the problem solving process. Identify what you cannot do.

You can also start looking somewhere in the middle of things. For example, you should presumably be counting somewhere. What value should you start counting from? How do you know when to stop counting? Can you write code that writes those numbers to screen to show that you’re able to obtain the right numbers? After that you can deal with how to put them together into a result.

Sorry for responding so late, my apologies.
Though you probably already solved it or you moved on. Here is some help.

We are going to think about you code in blocks

First your variable declarations:

max = 101
my_lsit = [ ]

Next you for loop:

for i in range(max):

my_list.append(start + 3)

We got rid of the return if the for loop


if(start > 100):

lst1 = [ ]
return lst

The goal we are trying to reach is:
[91, 94, 97, 100]

So what is happening in our code?
First, we will look at the for loop and see what it is doing.

In the for loop, the code in going to iterate 101 times! That is not what we want.
Additionally, it is going to add “start + 3” to the my_list variable 101 times. So your variable will look like this: [94, 94, 94, 94…] 101 times. That is definitely not what we want. However, that does not explain why we are returning “None”. Lets look at the next block.

In the next block of code we have a conditional statement:
If the start variable is greater than 100 than we return an empty list
Pretty straight forward. However, there is a hidden bug in this code. That is the placement of the conditional statement. Why? Well lets go through a little test.

Lets assume that our For Loop does exactly what we want it to do. If our start input is too high, do we want it go through the for loop any way? No, I does not meet our requirements from the start! So our first change is to put the conditional above the loop.

max = 101
my_lsit = [ ]

if(start > 100):

lst1 = [ ]
return lst

This makes sure that the program will end right when we enter a number bigger than 100.

Okay, lets move to the for loop.

We do not want the for loop to run 101 times.
This is a bit tricky. In the head of the for loop, when we input the range, we have a couple for options:

for x in range(beginning, ending(exclusive), skip):

What we want to see is:

for num in range(start, max, 3):

Why? Because we want to start at our input, end before our max, and skip every three numbers. We then want to add each of those numbers to our list.


We are now done! We now have:

max = 101
my_lsit = [ ]

if(start > 100):

lst1 = [ ]
return lst

for num in range(start, max, 3):


the last piece of the puzzle is to return my_list.

return my_list

The code will work. However, there is still one little clean up code I left there if you can find it.

I must admit, I never explained why the code returned “None” after you remove the “return” in the while loop.

Well, a console will return None if a function doesn’t return anything. Even if you have a print statement, the console doesn’t see it as you returning anything. It only sees that code is being executed.

That being said, when you exited the for loop, your “start” variable was still 94. Question, does you conditional statement stop the code? No. As a result, the code runs right on through to the end and you get a dud. Sadly, your code won’t tell you anything. This is what is called a Logic Error (At least I think so). The error is not with construction but with flow of the code. A simple analogy is “you turned left when you should have turned right.” The roads are perfectly fine depending on the directions you are given. If you are given the wrong direction, you get to an unintended destination.

I hope this was a clear enough explanation of the code. Please if there are any errors, add to it so that other people can benefit. Happy coding. :grin:

1 Like