Help

Hello, can someone translate this task please?

Create a function named double_index that has two parameters, a list named lst and a single number as index .

The function should return a new list where all elements are the same as in lst except for the element at index , which should be double the value of the element at index of lst .

If index is not a valid index, the function should return the original list.

and why is this the solution to it:

def double_index(lst, index):
if index >= len(lst):
return lst
else:
new_lst = lst[0:index]
new_lst.append(lst[index]*2)
new_lst = new_lst + lst[index+1:]
return new_lst

thanks

Hello,
can someone explain this task please?

Create a function named remove_middle which has three parameters named lst , start , and end .

The function should return a list where all elements in lst with an index between start and end (inclusive) have been removed.

and why this is the solution to it:
def remove_middle(lst, start, end):
return lst[:start] + lst[end+1:]

This will be a slice from the start of the list, up to but not including the element at index start.

This will be a slice from the list that excludes the element at index end and includes everything up to the end of the list.

When we combine (concatenate) the two slices, the specified middle range is excluded from the output.

i don’t get it, neither lst, start or end is defined, how is it still running it?
what if there is another list in there?

Those are already explanations in English, so what are you asking for? Better if you ask for specific information and/or tell what you don’t understand, than to ask for something to be more and better which doesn’t say what is insufficient about what you already have.

this is the part that i don’t get

Then, what you misunderstand is what a function does.

A function accepts some values, does some operations on it, and produces an output value.
In other words, a big part of what a function does is to delay evaluation until those values do exist.

So when you ask how it runs without inputs being defined - it doesn’t. All a function definition does is to say what happens when the input does arrive.

Sorry I really don’t get what you’re saying…
lst has nothing defined to it and neither do index

If you want to learn about how a function operates, then don’t put a bunch of complicated code in it to distract you.

Use something simple instead. Like addition.

def f(a, b):
    return a + b

This is the same situation, is it not? But by eliminating code that isn’t relevant to what you’re asking, suddenly it’s a whole lot simpler, you might even already understand everything going on here at this point. All that remains then, is to treat your other function the same way.

i didn’t add any of this, i clicked on solution and this is what came up, i couldn’t do it myself

Then I would argue that you should leverage what you know to bring yourself from the starting state to the desired state.

If you click the solution button then what’s the point? It’ll only get worse as you continue if you have no solid ground to stand on.

Most programming tasks are solved by combining many small operations into increasingly powerful/useful operations. Most tasks boil down to not much other than if-statements and loops. So if you know those, then you can solve many other tasks as well.

up to this point I did everything by myself except the random game task because that was too complex for the education the site gave me which is something that should be looked at…
it expects me to google everything myself and find solutions on other sites rather than teaching it and explaining
rather dissatisfied as it is 32 pounds a month

For most problems you will want to start by considering how the information needs to be processed. For example, if something is about a list, then you might write down a list on a piece of paper and process that list manually, analyzing what you do, break down into smaller steps. You may need to break down those steps into smaller steps and so on, until the parts are small enough that they match things you know how to use in code.

There’s too much to know to keep it all in your head. This is what you should do. Not to obtain finished solutions, but to find out how to carry out various operations so that you can then leverage them in your own code.

Not sure what that has to do with programming. Might have something to do with codecademy the company, but that isn’t me.

right,
can you answer if lst, start, end have to be defined or not?
what if theres:
numbers = [1, 5, 3, 2, 6]
items = [handle, vernier, ladder, knife]
will numbers automatically become lst1 and items become lst2?
is start automatically 1 for numbers and handle for items?
is end automatically 6 for numbers and knife for items?
thanks

where do a and b come from?
what is f?
how is f used?

have you used any other functions? how does that look like? how does that relate to f, a, b?

If f is a function, then what can you do with f, what can you do with a function?
Usually what you would do with a function is to call it. What does that involve?

for example you can do print(f(3, 5)) and will return 8 i believe

What happens with a and b when you do that?

it adds any two numbers you put in there together but can also predefine it i suppose

If, inside your function, you print out a and b
and you call that function as such:

f(3, 5)

Then what gets printed to screen?


so where then do lst start end come from?

this is the part that comes with the task:
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

but can’t get my head around why

def remove_middle(lst, start, end):
return lst[:start] + lst[end+1:]
returns this:
[4, 23, 42]