Question 14/18: Passing a range into a function


#1

So I have the answer to this problem, but I don't understand how I got it right in the first place, so somebody with knowledge on this please explain to me what I did to get this write.


P.S: Anybody who looked at this question, just to find the answer please try to understand why it's correct aswell.


#2

What exactly is it, you don't understand?

well, agree with me that that if do this:

print range(0,3)

that it results in: [0,1,2]. The first number in range indicates the start, and then it will increase in steps of one, while it less then the second number (3 in this case)

Now, i could also do:

for i in range(0,3):
   print i

which will result in:

0
1
2

agree? Now lets look at your for loop:

for i in range(0,len(x)):

len(x) is 3, since x is a list with 3 items, agree? Also agree that if you have a list with 3 items ([0,1,2]), the items are indexed at 0,1 and 2? So, now we have a loop which will gives: 0,1,2 which we can use to access the items in the array


#3

Thank you for responding sir, My problem is not understanding how to transfer the directions to Python. When if the program tells me to define and give an argument I understand that, but the following part I'm always confused on, and search for an answer from the forms. I do attempt to do it, but I would constantly get it incorrect without help.


#4

So, you need to write down for your self:
What do i want to achieve? How could i achieve this? Which functions do i know? Do i need loops, if/elif/else?

Write down the problem, break it down in steps. This is where programming get tricky. Following instructions: fine. Design your own program? Far more complicated, you need to a find method to solve this problem