13. Modify each element in a list in a function


#1

what should I do?


#2

It is an indentation problem. Your return statement's indentation is wrong. Instead of indenting twice, indent only once. Just backspace before the return statement.

It would look like this in total:

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

#3

If you indent it twice, it would be inside the for loop. But we need it inside the function. Indentations are really important in python, you know. :smile: I hope you understood, just ask if you didn't.


#4

Did you pass? Please tell me if you did. I am eager to know if it helped.

Waiting......

:smile:


#5

THX!!!!

it worked !!!!!

Really appreciate it


#6

Please mark it the solution so that other people having this problem can understand that this is the solution. Really happy to hear that it worked. :smile: :smile: :smile: :smile: :smile:


#7


#8

I thought there was a "mark solved" button or something to mark a reply solved. Is it not there? I'm telling because I've got some notifications that people had marked my post as solution don't check for it if this bothers you.


#9

Can u explain me a bit more about the indentation? I´m still struggling with that


#10

Indentation is no big deal. Just press the tab key to indent something. The thing is, it is very important in python. Even one missing indentation produces errors. I will explain:

In other programming languages they have an end statement for if statements, loops, functions etc. So, it will be like:

if # some condition

# some code

end if

Now, whatever comes inside these if, end if blocks will belong to the if. But, in languages like python which doesn't use end statements, indentation levels are used to understand under which statement a particular block of code belongs to. Like, for example:

for i in x:
    if i == t: # this if statement comes under the loop as it is indented 
               # after the loop
        # some code: this code comes under the if statement as it is indented
        # after the if
# if there were some code here, it would be outside the loop and the if
    # if it was here, it would be inside the loop

This is why it is important, misplacing your code will be of no use. If you placed it right, you could see it neat and understand where some code belongs to.

I hope you understood. Please tell if you didn't. :smile:


#11

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