14/18 Please tell me how I am wrong


#1



Could someone please explain what this one is asking me to do?
here are the instructions:
On line 6, replace the ____ with a range() that returns a list containing [0, 1, 2].


https://www.codecademy.com/courses/python-beginner-nzzVa/3/3?curriculum_id=4f89dab3d788890003000096

after defining n as [0,1,2]

Nothing should happen in this code as I read it. there's no referenced data to manipulate within the function if you pass it a range.

all I see is, for each item in x multiply it and store it back into x.
passing a list, instead of a range, with 'n' being = to [0,1,2] returns a result of [0,2,4] which is accepted as a good answer, even though it's not what was asked for.


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

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

This code gives the proper response, but is not accepted because I did not pass it a list containing [0,1,2]( note that I had to add the int() function because .5 * 2 equals 1.0 which is not a good answer either.

def my_function(x):
    for i in range(0, len(x)):
        x[i] = int(x[i] * 2)
    return x
n = [0,.5,1]
print my_function(n) # Add your range between the parentheses!

So, am I out of my gourd or is the question just wrong?
As I see it you cannot pass the [0,1,2] list it requires you to pass,
nor can you pass a range to the function as it says to, you have to pass a populated list for it to function,
and if you give it the data it says to, you get an incorrect returned value unless you change the code of the function.


#2

The built-in function range creates a list.
You're meant to call range with the appropriate arguments to make it produce the list [0, 1, 2]


#3

ok. taking that as you say. I read this:

define a function that accepts an input,x

for each item in a list made by the range function do this:

multiply x[item] by 2

then return x

at best you are returning the input list with each value multiplied by 2. you are not returning the list created by the range function.


#4

If you're changing the original list and then returning it, then you're returning the list created by the range function!


#5

can you explain how they are related?

we are asked to feed a list containing a range of numbers to a function.

the function iterated through the list using ANOTHER list created by the range function.

the two lists are different lists and you are not copying the data of one into the other based on the code.


#6

I feel like I am being difficult, but it makes no sense to me.
in fact the answer the system accepts in order to pass the section, isn't even the one it asks you to come up with.


#7

To access each position in the list you need indexes. Indexes start at 0 and count up with a step of 1 which is something that the range function can produce.

If you study the code, the list that is modified is the one referred to by x.

A better way to write that function if the original doesn't have to be modified is:

def my_function(x):
    return [n * 2 for n in x]

Which creates a new list populated with n * 2 for each n in x. Due to the existence of list comprehension the function probably shouldn't exist at all just because of how easy that is to write and read.


#8

I agree. That is what I have been saying.

but the result is not compatible with the question

the question asks you to replace the ____ with a range() that returns a list containing [0, 1, 2].

what would you put in place of ____

perhaps that will help me understand what you mean.


#9

Replace the underscores with a call to range


#10

just range() nothing else?


#11

Would that return the desired list?


#12

I don't see how. range() with no value passed to it should return an error.
but I will try it.


#13

If you don't see how it would return [0, 1, 2] then you probably shouldn't do it, you have no reason to.

Is there only one way to call that function, giving it no arguments? Maybe you should give it arguments that tell it to start at some value and end at another so that it produces the desired list.


#14

TypeError: range expected at least 1 arguments, got 0


#15

well the system has only shown me one way to use the range function, that is to create a list. doing what you are hinting at puts a [0,1,2] list into the my_function. what is returned from the function is [0,2,4] not the answer of [0,1,2] that the question asks to have returned.


#16

Have you read the instructions? They say quite a lot about how you can use range. Is there something you think is missing that you need? If so, what?

You have to consider when [0, 1, 2] should exist, is it really after your function has been applied to it? Why not before?

Instructions say:

with a range() that returns a list containing [0, 1, 2].

It's saying that range should return that list, not your other function.


#17

yes. that is not the problem as I see it.

the provided code is multiplying the list by two!

if the question wants me to return a list of [0,1,2] the I have to start with values that when doubled would be equal to [0,1,2], so since the range function cannot create a list of [0,0.5,1] I had to create a list with those values. using these values makes the code give the right answer but the system says it is wrong.

def my_function(x):
    for i in range(0, len(x)):
        x[i] = int(x[i] * 2)
    return x
n = [0,.5,1]
print my_function(n)

this returns the expected [0,1,2]


#18

is there something I am misreading and your just teasing me till I give up and quit?


#19

Instructions say that your range call should return [0, 1, 2]

with a range() that returns a list containing [0, 1, 2].

It does not say that my_function should return [0, 1, 2], I'm not sure where you are getting that from. Keep in mind that returning and printing are very different things.

That means that after doubling each element, you'll end up with [0, 2, 4]


#20

it's the only return statement in the code. that's why I thought that is what they were talking about.

thank you for clarifying that for me.