Alternate solution using range?


#1



So I know how to do this exercise (https://www.codecademy.com/courses/python-beginner-nzzVa/3/4?curriculum_id=4f89dab3d788890003000096) using just

for x in n:

However, I would like to try to pass this exercise by utilizing the range.

Could someone please let me know what am I still missing?

The most confusing part of my code might be

n[(len(n)-1)]+dif

The idea was to get the last number in the list. For instance, if n = [0,3,6]. Then len(n) is 3. 3-1 is 2. n[2] is 6 which is the last number in the list. Dif is the absolute value of (first item in list - second item in the list); therefore, 0-3 = -3 and absolute value of -3 is just 3. Thus, 6+3 is 9 and 6 will be now included in the output.


n = [3, 5, 7]

def total(numbers):
    result = 0
    dif = abs(n[0]-n[1])
    for x in range(n[0], n[(len(n)-1)]+dif,dif):
        result += x
    return result


#2

Hi @microcoder79525 ,

Here is the documentation for the range function: range(start, stop[, step]).

Your solution seems to be going in a more complicated direction than necessary. To simplify your solution, consider how the value returned by the len function relates to the stop value that you need to pass to the range function, as well as what is the index of the final item in the list that you are accessing.


#3

Hi @appylpye :slight_smile:

First of all many thanks for your reply. I really appreciate it though it's nothing new to me. I just don't see it or maybe I don't understand the range function that well :frowning:

To illustrate on n=[3,5,7]

  1. start should be n[0] as that takes the first item from the list. Therefore, in this case it would be 3.

  2. step should be the difference between any two numbers in the list (at least in this case). Just to ensure that the result isn't negative I am using the abs. Therefore, 3-5 is abs(-2) which is step of 2.

  3. stop should be 9 as from my understanding the stop is never reached. As for instance range(5) is 0,1,2,3, and 4 (not 5). The len(n) is 3 as there are 3 numbers in the list. As n[len(n)] is n[3]...and there isn't any index of 3 as 7 is index of 2, I need to put minus 1. Therefore, n[len(n)-1] should equal to 7. However, using stop at 7 would not include 7 and therefore I need to enlarge this by the difference of 2 which leads to 9.

Actually, just using print range (3,9,2) leads to the output of [3, 5, 7]. So I probably understand the range function.

Could you please give me another hint or point me where is my reasoning errorneous please?


#4

you are making it yourself super complicated. look:

n = [3,5,7]
for i in range(0,len(n),1):
    print i

i has the required indexes, you can just do n[i] to get the items from the list.

we start at index zero (arrays are zero index based), then we stop at length of the list (stop number not included) in steps of 1, since steps of 1 is the default, we can also leave it out


#5

Hmm, I am not sure I really understand.

Using your approach is basically range(0,3,1) which would be 0,1,2 which wouldn't help me to solve the problem. Or am I missing something here?

You see in the exercise I need to take all the numbers from n and add them together. Therefore, the easiest solution is just:

for number in n:

However, I wanted to try to solve it using the range as I described.


#6

0,1,2 is perfectly helping you? It matches the indexes of your items in your list, so then you can do n[i] to use the indexes to access the items of the list at those indexes?

remember how we use [] to access strings and list by index?

look:

n = [3,5,7]
print n[0]

that will print 3, this should have been covered in the course?


#7

Aha, I see what you mean :slight_smile: yes, it was covered. It is just that you wrote print i...not print n[i].

n = [3,5,7]
for i in range(0,len(n),1):
print n[i]

Thank you very much again! I really appreciate it!


#8

i already mentioned in my first answer i hold the indexes, but you seemed to have missed it.

I was trying to push you in the right direction, without fully disclosing the correct solution.

if you are lazy you can also write:

n = [3,5,7]
for i in range(len(n)):
    print n[i]

if range only has one argument, it is the stop value, and then the start value will be the default (zero) and steps are 1 by default


#9

Thank you very much again for showing me not only the correct solution but also another version of it. Really good to know how it works defaultly - haven't thought of that.

Now, I will just try to figure out what mistake I made in my own solution which as you said is unnecessarily complicated.


#10

Hi @microcoder79525 ,

See @stetim94's replies for some clues regarding what to do.

In particular:

  • You are using the variable, x, in the loop header, which is fine. Now, within the loop, you should use x as an index to access each item in the numbers list with square brackets.
  • Simplify your call to the range function. The start value should be 0, so that you can begin at the first item in the list. The index of the final item of a list is 1 less than the len of the list, therefore you can use the len of the list as the stop value here. Since you need to access every item in the numbers list, the step should be 1. With a start value of 0 and a step of 1, you can call the range function with a single argument, which is the stop value, and as above, is the len of the list.

#11

Hi @appylpye and @stetim94,

Ok, I finally found out why my solution didn't work :smile: In the beginning I noticed that codeacademy is testing the code against list which have same steps. For instance, against [3,5,7] or [0,3,6] and thus I created specific code which was counting on any set of numbers but with same step (at least I hope the code I wrote achieves this).

Only later, I noticed that codeacedemy is testing the code against any list of numbers :slight_smile:

Anyhow, thank you both again!


#12

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