Act fast, special offers end soon! Up to \$294 is savings when you get Premium today. # I don't understand why my solution is right but is not accepted... Maybe something I don't see?

Screen Link: Learn data science with Python and R projects

``````values = [16, 1, 7, 2, 19, 12, 5, 20, 2, 10, 10, 14, 17, 14, 1, 16, 19, 7, 9, 19]
``````

My Code:

``````for number in values:
print(number + 1)
``````

What I expected to happen:

To increase each value contained in the values list by 1

What actually happened:

``````17
2
8
3
20
13
6
21
3
11
11
15
18
15
2
17
20
8
10

``````
``````* Value of values is not what we expected.
* One of your variables doesn't seem to have the correct value. Please re-check the instructions and your code.
``````

@jsmillon look at the line `print(number + 1)`. What is that doing?

Hi @jsmillon and welcome back to the community!

Although the output you’re producing is correct, what the exercise is asking is for you to update the variable `values` so that each element has been increased by `1`…the `print()` function does not change the values within `values`. If you use the variable inspector you can check that `values` is unchanged.

Hint: use list indexing in a `for` loop on `range(len(values))`

1 Like

Hi!

Thanks for the reply, but I still don’t understand the code.

No worries, @jsmillon! Let’s take a look at an example in order to see the difference between printing a calculation on each element of a list vs updating each element of a list with a calculation. Let’s say we start with a list of values like:

``````test = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
``````

If we were to use a loop in order to access each value in the list and then print out the results after doing a calculation on each value, it would not change the original values of the list even though the Output would look like our desired result:

``````for i in range(len(test)):
print(test[i] ** 2)

Output:
0
1
1
4
9
25
64
169
441
1156
``````

While this might be our desired output, the original variable (`test`) has not been changed at all!

``````print(test)

Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
``````

The reason being: we didn’t save/update the values in `test`…we just accessed the value, did an operation on it, then printed it to the console which leaves `test` unchanged. But what if we wanted to store these values back to the original list? That would look something like this:

``````for i in range(len(test)):
test[i] = test[i] **2
``````

Now when we check on the values for `test` we can see they have been changed:

``````print(test)

Output:
[0, 1, 1, 4, 9, 25, 64, 169, 441, 1156]
``````

Does that make any more sense? Let me know where you are getting stuck and we can work on it together some more.

Happy learning!

1 Like

Hi,

Thanks for the lengthy explanation! I think I can see the difference between my code and the correct one. Still, I get stuck in the range(len(values)) part Whenever I get stuck on something like this, I like reading the documentation for each function (len() and range()) and then play around with each one separately to get familiar with what they do before trying to figure out what they do together.

1 Like