I don’t even know what the documentation is, this term wasn’t mentioned in courses so far. As I’ve said, this is my first contact with programming. With such high price I trusted that it will provide me with everything I need to know.
I confess I myself did not consider not using an if statement and it may not have occurred to me even if I knew of the built-in range function (in particular the interval parameter of the function).
But to be fair to the new learners, the range function has not presented by this point in the course (does not excuse not going through documentation but if you are a new learner it’s only natural to go through lessons rather than dive into PY documentation without context) so there’s an availability heuristic at play - if the only tools you know of are hammers, you are going to use the hammer to tighten a screw.
It’s probably the biggest difficulty in teaching coding - how to teach the intuition you described rather than the magic syntax words.
I can’t seem to find the solutions on the website:
#Write your function here
def every_three_nums(start):
if start > 100:
return []
else:
return range(start, 101, 3)
#Uncomment the line below when your function is done
print(every_three_nums(91))
My code prints range(91, 101, 3) instead of [91, 94, 97, 100] on the website. I can’t figure out why
def every_three_nums(start):
if start > 100:
return
else:
return list(range(start,101,3))
u should cast it to list
In the now defunct Python 2, range()
would return a list
. In Python 3 it now returns a range object
.
Now consider,
>>> n = 120
>>> range(n, 101, 3)
range(120, 101, 3)
>>> list(range(n, 101, 3))
[]
>>>
Notice that we don’t need to test n
?
Thanks mtf, I was using:
http://www.pythontutor.com/visualize.html#mode=display
to visualize the code and I guess it is running Python 2 as range returned a list so I was confused.
another way to solved it (after googling a bit) was to unpack range using [*range(n, 101, 3)]
Hi,
for me it worked like that:
def every_three_nums(start):
a =
if start > 100:
return(a)
else:
return(list(range(start, 101, 3)))
print(every_three_nums(91))
Hello everyone,
For me, it worked like that:
def every_three_nums (start):
if start <= 100:
return list(range(start, 101, 3))
else:
return []
start being greater than 100 isn’t a special case you can remove that…
You do still need to account for the empty list being returned:
def every_three_nums(start):
if start <= 100:
return list(range(start, 101, 3))
else:
return
100
is a legitimate input so why should we omit that one value?
>>> def every_three_nums(start):
return list(range(start, 101, 3))
>>> every_three_nums(100)
[100]
>>>
It’s not like we’re actually going to use 100
if we want a sequence. Still, a sequence with length 1
is an iterable. In this case there would be only one iteration of that sequence.
>>> every_three_nums(1)
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97, 100]
>>>
100
is in that list so why shouldn’t it be in the first one?
You omit it because it says to return an empty list. It doesn’t say to return the value. In a real world scenario I’d totally be on board with you but in terms of the practice problem, I was just writing what it was asking for.
IF the value entered is greater than 100.
>>> every_three_nums(101)
[]
>>>
Does it say to eliminate 100
?
100 is supposed to be inclusive in the list. It’s not being omitted because it’s supposed to be included which is why I had the <= sign in my code. It’s only when it’s over that that an empty list would be returned.
That’s my point. 100
is a valid input, and we don’t need to check for it. The range takes care of that. We don’t need that if statement.
Ohh, ok. I must’ve misread the initial reply. Sorry about that!