# FAQ: Learn Python: Loops - Review

#1

This community-built FAQ covers the “Review” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#2

I notice my knowledge in this is vastly dwindling due to how the instructions is describing what it wants me to do. Furthermore, I am using more hints and conclusions than I care to admit which means I am not learning much. The critical thing next is likely going to cause me to have a stroke. Is there any information out there that can be of better help to understand so I can come back and continue this?

3 Likes
#3

I am having this same challenge.

1 Like
#4

Hi Guys

Just completed this exercise and had question regarding my code

single_digits = range(0,10)

for digits in single_digits:
squares =
for digits in single_digits:
squares.append(digits**2)
print(digits)

print(squares)

cubes = [digits ** 3 for digits in single_digits]

print(cubes)

Seems like I had to start another for loop in order to get the correct list squared (otherwise I just return 81 - if second for statemtent is ##)

New to all of this and cant quite see why this is the case.

Help greatly appreciated

#5

Have the same question as yours… also curious why it’s returning the squared value of the last number.

#6

It seems we are all in the same both, sometimes I feel so upset that I wanna break my laptop whenever is read the instructions, so confusing

#7

Let’s have a look at your code to see where the problem is arising.

For step one we can either manually build the list,

single_digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

or assign a range,

single_digits = range(10)

The second step asks us to print each item in the list using a loop:

for x in single_digits:
print(x)

The third step asks for an empty list…

squares = []

Write that line above the loop, then move on to step 4.

for x in single_digits:
print(x)
squares.append(x ** 2)

Next we will print the squares list.

print(squares)

Step 6 assumes we know how to create a comprehension…

cubes = [x ** 3 for x in single_digits]

and finally, print that list…

print(cubes)
#8

Hi Roy,

Thanks for your explanation. This is exactly how I wrote the code, but it didn’t woek. When I checked the solution, I found out that the empty square list was printed first whish is not the way the instruction was constructed.

I have to write it the way it was written in the solution before it worked. Mos of the few obstacles I have had with Codecademy tasks and quizzes are the way the instructions /questions are been made.

Thanks Roy for your usual help here

1 Like
#9

I had a bit of fun with this one:

single_digits = list(range(0,10))
print(single_digits)

squares=[]
cubes=[]
for i in single_digits:
squares.append(i**2)
print("the square of "  + str(i) + " is " + str(squares[-1]))
cubes.append(i**3)
print("the cube  of " + str(i) + " is " + str(cubes[-1]))

sample output

the square of 6 is 36
the cube of 6 is 216
the square of 7 is 49
the cube of 7 is 343

Enjoy!

1 Like
#10

Couple of things…

1. If we only wish to iterate and not access elements in a range it does not need to be cast as a list.

2. 0 is the default start value so we don’t need to write it. Mind we don’t need to see what 0**2 and 0**3 are, so we can start with 1, or even 2.

for i in range(2,10):

3. String formatting makes our code more readable and less wieldly.

print ("The square of {x} is {y}".format(x=i, y=squares[-1])

1 Like
#11

1 Like
#12

…Starting from Loops, whole course is very poorly written.
Python 3 course at CA have issues in previous lessons too, but starting from chapter 5 it turns to playing games and trics with student’s mind.
Additionally, they don’t use PyCharm as workframe for their outside lessons what is frustrating.

Loops are one of the simplest topics (because it’s standard for / while / break / continue conditions), but not here.

As example, in 5.1.11 if fact they ask student to do range(10), but by request description it want to make xxx = [1, 2, …10].
And etc.

#13

Apart from others, what I’ve realised so far about loops, is that great attention must be paid when we use “print”. Sometimes it is necessary to “print” inside the loop with indentation, but in other cases this brings trouble and “print” should be written outside loop without indentation.

To be more precise, about the loop with digits:
for item in single_digits:
…print(item)
…squares.append(item**2)
print(squares)
Here, the output seems normal. The first print(inside loop), gives the items from 0 to 9 in a vertical order and then, the second print (outside loop) gives the all squares in a list [0,1,4,9,16,…81]

However, if both “print” are inside loop, the output is complex:
0
[0]
1
[0, 1]
2
[0, 1, 4]
3
[0, 1, 4, 9] and so on…

I suppose this is because of the iterations. It is like every iteration in the loop consists of two “sub-iterations”, one for the print items and one for the print squares. Is it true?
If this is the case, why the result of the second “sub-iteration” is a list including all the squares so far and not the result of the squared item? i.e.

2
4
3
9
4
16…