FAQ: Code Challenge: Lists - Every Three Nums

This community-built FAQ covers the “Every Three Nums” exercise from the lesson “Code Challenge: Lists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Every Three Nums

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was split to a new topic: List Challenge Feedback

6 posts were split to a new topic: How should I check if start is greater than 100?

A post was split to a new topic: Solving without the range function

3 posts were split to a new topic: Why do I get index out of range?

A post was merged into an existing topic: List Challenge Feedback

2 posts were split to a new topic: Should my list always end with 100?

I cannot seem to finish this exercise, even though according to knowledge I found online my syntax is ok, I think. This is my code:

def every_three_nums(start):
notempty = range(start, 101, 3)
if start > 100:
return empty
return notempty

And it gives me this error:
every_three_nums(91) should have returned [91, 94, 97, 100] , and it returned range(91, 101, 3)

In Python 3 this does not return a list, but a range object. To return a list, we will need to cast it using the constructor…

list(range(start, 101, 3))

additionally the range would be empty if it was… empty. no point testing since you’d use the range either way.

1 Like

Im sorry but I don’t understand your post ionatan. Range would empty if it was what? And testing what exactly?

empty: size 0

you’re only testing one thing, see your if statement, you’re testing whether the result is empty

Exercise told me to return different result depending from “start”. If it’s empty I have to return a different value than if it’s not empty. So far in this course I have not been thaught any other method of testing. Is there any other method of doing this without testing?

There’s no reason to test if you use range
You’d return the range.

Note that what your function does is to create a range. That’s what range does, range fully implements this already, there is no more work to be carried out, you would do only this:

def f(start):
    return range(start, 101, 3)

IF you use range, then anything in addition to this is doing nothing.
(the point of the exercise is probably to implement it yourself though, so that would mean doing it without range ie. you’d start with… start, and an empty list, and then begin appending values to your list and incrementing the current value by 3 each time and repeating this until you reach the upper bound at which point you’d be done and ready to present the result)

Exercise specifically tells me to use range BUT it also tells me this:

" If start is greater than 100 , the function should return an empty list."

That says how the function should behave, it doesn’t say anything about requiring code to be dedicated to it.
Since range already behaves that way there would be nothing further you would need to do

Oh, that’s good to know, thanks. I don’t think this was covered in any lesson so far. I also checked Python 3 lists PDF we got in the end of lists lessons and this info is also not there.

The thing to do is try it…

>>> range(101,101,3)
range(101, 101, 3)
>>> list(range(101,101,3))

Well I mean shouldn’t a user of range first consider what it does?
It’s not a special case, that’s at the core of what the function does

Hey, all numbers less than 101, starting at 500. Okay, that’s empty.

Yeah I see how it could be overlooked, but when the question is raised … compare it to what the function says it does

1 Like

Especially when a learner is glossing over the documentation and expects everything to be spelled out in the lesson. We have yet to see new learners do this exercise without using an if statement. Perhaps if the lesson spelled out things as you have above, it would foster greater intuition.

1 Like