Python 3: Advanced Python Code Challenges: Lists - range() problem

Edit: Two seconds after posting this and editing it, I realized my mistake. No assistance needed. For future people searching out this problem: The secret is list conversion.
Exercise:
https://www.codecademy.com/courses/learn-python-3/articles/advanced-python-code-challenges-lists

My code:
def every_three_nums(start): if start < 100: my_range = range(start, 101, 3) return my_range else: return []
(Edit: Apparently I’ve forgotten how to quote code on these forums. and retain the proper indentations. I included a screen capture.)

When I test test this code, it doesn’t print a list to the console, it prints range(91, 100, 3). The way I understand range(), it’s supposed to generate a list and return that list. Can anybody give me some guidance on what I might be overlooking with this exercise?

2 Likes

Glad you sorted your issue. If you’re looking for formatting guidance this FAQ covers a lot of it-

For anyone else who comes across this and scratches their head range used to return a list in python2. Now it returns an iterable range object (an immutable, low memory sequence-like object). It’s not an iterator so it can’t be exhausted and it supports certain actions common to sequences like slicing and containment testing but not others. The docs have some nice examples-
https://docs.python.org/3/library/stdtypes.html#range

As per @thegovernment0usa’s comment- If you need a list in python3 you’ll have to covnert it.

2 Likes

Hello, @thegovernment0usa.

print(every_three_nums(100)) should print [100] not [].

There is no need to test the value of start. The arguments supplied to range() will only return values within the range. If start isn’t within range, and you use the list constructor, you’ll get an empty list:

a = 10
print(list(range(a, 10, 2))) #[]
a = 5
print(list(range(a, 10, 2))) #[5, 7, 9]
1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.