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:

Computer Science
Data Science

FAQs on the exercise Review

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

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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

I am having this same challenge.


#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 :confused:

Can anyone please help us @mtf where are you? You are my knight in shining armour here on Codecademy, you wanna save us, please?


#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


#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!


#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])


#11

Thanks for the tip/follow through!


#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.