FAQ: Code Challenge: Lists - Every Three Nums

Community%20FAQs%20on%20Codecademy%20Exercises

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!

This is a very frustrating. I hate being quizzed on things that were never addressed during the lesson. Unless you know of the range() function before starting, just use the “hint” button. This should be incorporated as part of the learning journey… or maybe at the end say look up “range() and int().” “int()” was not included as part of this review either.

6 Likes

I looked up range and used one of the examples i found. I’m pretty sure I have cheated here by setting my second value to 103 ; so i am interested in what the real solution should look like.

def every_three_nums(start):
  if start > 100:
    return []
  else:
    list_of_ints = list(range(start,103,3))
    return list_of_ints

#Uncomment the line below when your function is done
print(every_three_nums(91))
1 Like

I solved it with this function:

def every_three_nums(start):
return list(range(start, 101, 3))

#Uncomment the line below when your function is done
print(every_three_nums(91))

3 Likes

What is curious is that the correct answer/solution does not address the last function requirement.

Ifstartis greater than100, the function should return an empty list.

My code does address this:

def every_three_nums(start):
  """function should return a list of every third number between\n start and 100 (inclusive). For example, every_three_nums(91) should return the list [91, 94, 97, 100]. If start is greater than 100, the function should return an empty list."""
  return list(range(start,101,3))
  if start>100:
    return []

print(every_three_nums(91))
print(every_three_nums(191))
1 Like

Hi @pleabargain,

Your code doesn’t actually need this if block:

  if start>100:
    return []

If the start argument passed to the range constructor is equal to or greater than the end argument, and the step argument is positive, the result is an empty range object. In the current case, this occurs if the start value is greater than 100.

That is sufficient to enable the first return statement within your every_three_nums function, by itself, to satisfy this requirement:

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

2 Likes

You can code the empty list but it’s not necessary. Using the range function will return an empty list if the number is above 100.
Try this and you will see:

def every_three_nums(start):
return list(range(start, 101, 3))

print(every_three_nums(101))

2 Likes

I forgot about the range function. I still managed to solve it; but now, seeing the one-line solution above, I realize I really overthought this one:

#Write your function here
def every_three_nums(start):
# creates an empty list
  new_lst = []
# returns empty list if start is more than 100
  if start > 100:
    return new_lst
# otherwise new_lst starts at 'start'
  else:
    new_lst = [start]
# as long as the last number of the list is no more than 97...
    while new_lst[-1] <=97:
# ...the function adds a number equal to the length of the list times 3 plus 'start'
      new_lst.append(len(new_lst)*3 + start)
    return new_lst

Hello, I am just starting out python, and I know my question may be stupid. But, why am i getting this error? “IndexError: list index out of range”

def every_three_nums(start):
new_lst =
if start > 100:
return
else:
for i in range(start, 103, 3):
new_lst = new_lst[i]
return new_lst

def every_three_nums(start):
  new_lst = []
  if start > 100:
    return []
  else:
    for i in range(start, 103, 3):
      new_lst = new_lst[i]
  return new_lst

#Uncomment the line below when your function is done
print(every_three_nums(91))

thanx . that was helpful