14. Passing a range into a function -- return [0,1,2] from range() not from my_function()


#1

In the instructions you state:

On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2].

The answer ends up begin to pass the following statement:

print my_function([0,1,2])

But that is not what the instructions said. It wants us to pass a range that RETURNS a list containing [0,1,2]

This is very confusing and should be corrected.


#2

Not really a bug.

Yes, you were asked to use range. You decided to bypass this step by writing this list. It's not a problem with exercise.

Just try to follow the instructions and use range (yes, it's possible). All tests that codecademy executes to check your code are only an addition to your own self-criticism :panda_face:


#3

I found this very confusing as well. Turns out I didn't notice that range() returns a list up to but not including the parameter for stop as specified below.

Using range(start, stop, step) as a model I tried:

print my_function(range(0,2,1)) # Add your range between the parentheses!

it returned [0, 2] and an error message not [0, 2, 4] as I expected.

After testing the range() function I caught on that it does not include the 'stop' integer.
example: range(0,4,1) returns [0, 1, 2, 3]

I was able to find the solution passing only one parameter into range()

print my_function(range(3)) # Add your range between the parentheses!

#4

wow we have some smart folk in da room, please guyz keep it up.


#5

I guess the instructions should tell us to modify code so as to get a print of list [0,2,4] instead of [0,1,2].
Is there any way we can get a list of [0,1,2] printed out just by calling the function with a range?

I passed the section when the program printed out [0,2,4]


#6

this is a bug "On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2]." you cant edit the line to get a return of 0,1,2 either change it to "send a list with 0,1,2" or "returns [0,2,4]"


#7

You can actually return [0,1,2] by using range(0,1.5,0.5). But in this case they want you to actually input [0,1,2] instead of getting that as the answer. Seems like a typo in the question


#8

He's referring to the question. It should have said INSERT a list... instead of RETURN


#9

@scriptrunner78282 @plujer @tagpro56183
Range is a function. The output of this function is returned.
(Hence you using the return keyword in your functions)

You call a range to output [0, 1, 2] like this:

function_name(range(0, 3))

More on range.


#10

So here is one code that worked:

def my_function(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x

Add your range between the parentheses!

print my_function(range(0, 3, 1))


#11

This might not be a "bug" but the instructions vs. the result are terrible.

I just entered range 3 got a result of [0,2,4] which is NOT the answer the question is looking for and I got a "HIGH FIVE WAY TO GO!"

This is not a bug, its just terrible design. Several in this Lists and Functions are written this way. Clearly things changed, no one cared enough to make sure the questions, text, and answers still lined up.


#12

def my_function(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x

print my_function range[0, 1, 2]

anyone plz help


#13

I tried something like that but python gives the error TypeError: range() integer step argument expected, got float. So you can't use floats in ranges, I think. Btw after I read some this post, I went back to the exercise and it had completed itself for some reason?! this needs to be checked and fixed!


#14

There is a bug in this lesson. You [Codecademy] should not have answered that there's no problem or no bug in this lesson. I've noticed many of the lessons aren't copy edited very well and have a lot of mistakes or confusing instructions in general -- and not just in this Python unit.

But, the instructions for lesson 14 "Passing a range into a function" in the Python unit read to pass in a RANGE that will RETURN the list x = [0,1,2].

The function's body is defined as x[i] *= 2 and the range answer given by the Codecademy program is range(0,1,2), that is start at index 0, stop before index 1, and step by 2.

That can only result in a list of x = [0]. Pass in the range starting at 0, which is 0, times 2 = 0, stop before the next index of 1. The loop now stops and you get a list of [0]. The step parameter never executes because the answer the Codecademy program gives stops BEFORE index 1.

Changing the range to range(0, 3, 0.5) , that is step by 1/2, and you get an error that the range method can't accept a float as a step -- only integers.

Changing the function's body to be x[i] *= 1, and the range to be passed MUST be range(0,3,1) -- that is start at index 0, stop at index 2 and before 3, and step by 1's. For with a range of range(0,1,2), which is the answer the Codecademy program gives, you will NEVER get more than one item in the list x -- you're starting at index 0 and stopping before index 1. You will NEVER get a list of three items this way, regardless of what list you pass in, what the body of the function is, and not being able to pass floats. Even if you pass in a list set and change the body of the function to produce a list of [0,1,2], since the returned range and list stops before index 1, you will ALWAYS only get a list of ONE ITEM, like x = [0].

Either this question isn't written correctly or the answer that pops up in red when one puts in a 'wrong' answer isn't written correctly. One or the other needs to be changed. There's no way the range can be range(0,1,2), regardless if the program will accept range(3) or range(0,3). If the latter is the correct answer, then the answer given by the program is totally wrong.


#15

The instruction says:

On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2].

Function range() returns a list of numbers. User job is to find the correct parameters to make range() return a list [0, 1, 2]. So we want to start at 0, end before 3 and increment value by 1. It should be pretty clear that correct answer is range(0, 3, 1), range(0, 3) or range(3).

No one said that the correct answer is range(0, 1, 2). And I have no idea why you would like to pass a float as a step parameter.

Body of the function my_function() is completely irrelevant.


#16

If you pass in range(3) or range(0,3), you get a list of [0, 2, 4], when the you yourself, factoradic, just quoted that the question reads to pass a range that RETURNS LIST [0,1,2], but that the answer is to pass range(3). You're not reading your own instructions correctly. This question is not written correctly.

In order to get this function to return a list of [0,1,2] while passing in range(3), you HAVE TO change the body of the function to be x[i] = x[i] * 1, not x[i] = x[i] * 2 as it currently reads. Mathematically, you can't get a list of [0,1,2] from 0*2, 1*2, and then 2*2. You didn't trace the proper results of your function and write the question accordingly.

The only way to get the list the question reads to create would be to step by a 1/2-step, hence step by the float 0.5. Then tracing your function as written you would get 0*2 = 0, 0.5*2 =1, 1*2 = 2. But, then, either Python has have to an overloaded method for range() or one would have to write one's own function to overload range(), allowing it to accept floats.

Once putting in the float as the step in the range, you get a red pop-up dialog from your program reading "Oops, looks like you didn't pass in the range(0,1,2)," (besides the Python compiler error). That's quoting your program verbatim.

If you pass in range(0,1,2) you will only ever get x = [0] as that range is written. Even changing the body of the function to get something other than [0], you WILL ONLY EVER GET A LIST OF ONE ELEMENT. You CAN'T EVER get a list of [0,1,2] or even more thane one element if you start at index 0, and stop before index 1 as the read pop-up dialog states to do.

Obviously, the both the dialog with the hint/correct answer and the way the question is written is wrong.

To write the question correctly, you would HAVE TO change the directions to read. "Pass in a range that will return the list [0,2,4]," and when a wrong answer is inputted by the user, change the RED DIALOG to read, "Oops, looks like you didn't pass in range(3)."

--OR--

You would HAVE TO change the directions to read. "Pass in a range that will return the list [0]," and leave the RED DIALOG as is. Although, then, the program would have to be programmed to accept any answer, not just range(0,1,2) that gives list [0], like range(1) or range(0,1). If you're only looping once with the range, a step or an increment is superfluous.

This question is not only not clear, it's plainly written incorrectly.


#17

Let's make clear one thing - I don't like codecademy. Content is not consistent with the official documentations, they don't respect style guides etc... The difference between me and you is that I can easily prove my point by listing exercises and quizzes where content is simply wrong or misleading.

In case of this exercise... you just really need to read the instruction again.


I am surprised but it looks like it's the right time to quote myself.

Body of the function my_function() is completely irrelevant (to instructions).

I will try to use your style of writing to make it more straightforward:

the question reads to pass a RANGE that RETURNS LIST [0,1,2]

In summary, they want you to pass a range that returns list [0, 1, 2] and then, the correct output of function my_function is [0, 2, 4].

So, the correct solution is to pass as a parameter range(3) and then you get output [0, 2, 4].


Now, few words about error message:

It looks like you didn't pass a range consisting of 0, 1, 2 to my_function.

It simply means that your range does not return [0, 1, 2].


On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2].

range should return [0, 1, 2], not my_function with this range as a parameter.


#18

"Style guides".

GUIDES.
Don't try to fight again saying that styling is something required.

If it was inconsistent, your code wouldn't run in an interpreter.

Go ahead sir.


#19

Well, I don't like your attitude, but why not? Let's check if you are able to defend :slight_smile:

I will post five exemplary errors in codecademy content. You will prove that one of them is not true and I will apologize for my words. In contrary situation... well, I don't really need anything from you.

Let's start from quizzes, they are intended to be a summary of courses, right?


https://www.codecademy.com/en/skills/uk-quizzes/topics/uk-html-basics/html-basics-1/1

Small one. Implying that every HTML element has an end tag and content inside... A good example is an img element.

Documentation -> https://www.w3.org/TR/html-markup/elements.html


https://www.codecademy.com/en/skills/uk-quizzes/topics/uk-css-basics/css-overview-2/4

I love this one. It's so clear that codecademy content creators are taking information from w3schools, it's pathetic.

Order of values in the border shorthand property is completely irrelevant.

Documentation -> https://www.w3.org/TR/css3-border/#the-border-shorthands


https://www.codecademy.com/en/skills/uk-quizzes/topics/uk-css-basics/css-selectors-2/3

Two in one!

first-letter is a pseudo element, not a pseudo-class and pseudo-elements should be preceded by double colon. It's hilarious because this answer is there, but it's simply considered wrong.

Documentation -> https://www.w3.org/TR/css3-selectors/#first-letter


https://www.codecademy.com/en/skills/uk-quizzes/topics/uk-python-basics-1/python-syntax-1

Correct answer is obviously:

# First line
# Second line

Because you should know that """This is a multiline comment""" is a docstring, not a comment.

Documentation -> https://www.python.org/dev/peps/pep-0008/#block-comments


https://www.codecademy.com/en/skills/uk-quizzes/topics/uk-python-basics-2/python-bitwise-operators-2/1

According to codecademy the correct answer is XOR. And obviously it's a unary operator :smiley:

Do I have to add documentation reference to this one?


#20

@factoradic: For someone who doesn't like CC, you've spent enough time to get some badges, huh?
Either way, not everything is perfect and getting all defensive about it sure won't help anyone, probably just cause some problems and due to that the moderators will have to come and do something about it but we don't want to reach that point, do we? :slight_smile:

I'll talk about Python since it's something I'm more familiar with.
1) Documentation strings: it's true their "correct name" is 'docstring' and saying they're just comments isn't really wrong, but not entirely right.
Docstrings are "for those people who are going to be using your code without needing or wanting to know how it works. Docstrings can be turned into actual documentation. Consider the official Python documentation - What's available in each library and how to use it, no implementation details." - source
For now it would be easier for new learners to understand it as a "simple" multiline comment, I've personally used it once in my lifetime and there was no actual problem with calling it as a multiline comment but maybe in a test that talks about design coding or something related, it could be a problem.

2) XOR: I can't seem to find what's wrong in this segment so mind to say what you saw it as being wrong ?

So yeah, talk with the moderators and tell them the things that should be fixed so the students won't learn things "wrong".
And yes, I'm more than willing to talk and see your points so we can have a better website :v: