13/18 Question


#1


The code below is generating the error message "Oops, try again. double_list([0, 1]) returned 2 instead of [0, 2]"

What am I doing wrong?


n = [3, 5, 7]

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


#2

@gigaplayer53733

n = [3, 5, 7]

def double_list(x):
    d_list=[]
    for i in range(0,len(x)):
        d_list.append(x[i]*2)
    return d_list
    
print double_list(n)

#3

Thank you. I didn't know if the site would let me use ".append" in this instance.


#4

Sir, I have no idea how you've gotten that question right. The only reason why I passed this problem is copying and pasting your answer into the question. Can you explain to me step by step how I should get this question, please?


#5

@drewancfoxgmail.com,

# creating a VARIABLE =n= and =assign= a so-called =list= with 3 Elements
n = [3, 5, 7]

def double_list(x):
    # defining a FUNCTION =double_list= which takes 1 PARAMETER =x=
    # this =x= PARAMETER is used as a VARIABLE throughout the FUNCTION
    #
    # creating a VARIABLE =d_list= and =assign= an EMPTY=List=
    # we are going to use this =d_list= to capture the =Results= 
    d_list=[]
    # With the FOR-IN loop we prepare an =iterator=
    # where a VARIABLE =i= will get a VALUE at every =iteration=
    # the VALUE's will be 
    # in the RANGE from 0 untill the LENGTH of the =list=  
    # which we receive at 
    #  calling the =call= of the FUNCTION


    for i in range(0,len(x)):
        # as have to give an expression of a code-block, 
        # we indent with 4 spaces
        # 
        # x[0] will represent 
        # the 1st Element-VALUE of the =list= received at FUNCTION =call=
        # we MULTIPLY the Element-VALUE with 2
        # and APPEND the resulting VALUE into the pre-defined =d_list=
        # therewith creating an Element in this =list=
        d_list.append(x[i]*2)
        # next iteration will be done, 
        # this is also the END 
        # of the code-block associated with the FOR-IN loop
    # We have now skipped the 4-space indent,
    #  as we have ENDED the FOR-IN Loop
    #
    # we will now use the RETURN statement 
    # to return the RESULT of the FUNCTION-execution
    # A RETURN statement
    #   - will RETURN a RESULT with NO-DISPLAY
    #   - will EXIT the FUNCTION 
    #     (any folowing statement will NOT be executed)
    return d_list


# We now =call= the =double_list= FUNCTION
# and as the =double_list= FUNCTION was defined as having 1 PARAMETER
# we are providing 1 ARGUMENT being the VARIABLE =n=
# which represents a =list=
print double_list(n)

Also read the =function talk= in a separate POST......


#6

@drewancfoxgmail.com,

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )

#7

**

WOW.

**
thanks A LOT !


#8

Very Thanks !! , I am very novel whit Python and I am not more young ... . My English is very poor ...
I will asked your if you have a Web or Blog where you speak about Python .

agur


#9

They didn't tell me to use append and they don't say create a new list.Why do i use?


#10

Sometimes, there are multiply ways to solve a problem, and leonhards solution work, but it seems to be slightly in conflict with the instructions. I would personally update the items in the list, like the exercise is telling you


#11

You are right . But they don't told me to solve this solve problem on my way.

n = [0,1,2]
def double_list(x):
    for i in range(0, len(n)):
        x[i] = x[i] * 2    
# Don't forget to return your new list!
    return x
print double_list(n)

what's the wrong with the code?


#12

the exercise runs a few different lists, to test if your code is good, so i would check the length of the list you pass into the function (len(x)), rather then len(n), since the exercise might test a list with a different length then the one you created on the first line


#13

Now works .Thanks .Yeah, they test some list.But I have passed all the test.at last it show me list index out of range.why this problem occurs .is it my code problem or codecademy problem?


#14

Well, your list had 3 items (so a length of 3), where codecademy might test a list with 2 items, which means the third is a indexerror. I think the problem is mostly yours (no offense), but if you later decide to pass in another list (you can re-use functions), which has a different length, there is a problem, so codecademy tries to teach you, to write really good working bug free code.


#15

thanks.I got that.:slight_smile: