Is my code really correct?


Hello, I am a bit confuded and do not really understand this exercise. I didn't manage to pass the exercise just with the 'while' loop so I created a for loop inside of it and it tells me that the exercise if correct but if i change the range of my for loop to 20 for example, it's gonna print all the squared number from 1 to 20 without taking care of the limit set by the while loop. Could somebody please show me the correct code and help me to understand how it really works? Thanks!

num = 1

while num<11:  # Fill in the condition
    # Print num squared
    # Increment num (make sure to do this!)
    for num in range (1,20):
        print num**2


You can get rid of this line and it would work.


Beg to differ.

while num < 11:

11 is nowhere near 20. Either will work if coded correctly.

num = 1
while num < 21:
    # code
    num += 1


for num in range(1, 21):
    # code


For some reason,

num = 1

while (num < 11):  # Fill in the condition
    # Print num squared
    print num ** 2
    num += 1
    # Increment num (make sure to do this!)

this is my code and it works


Helps when one reads the lesson. The desired result is the double of numbers 1 thru 10.

So change the for loop to,

for num in range(1, 11):
    # code

And, the while loop is fine, as is (once corrected). Nothing to differ over, it would seem. Pardon me.


You're fine! I just want to always make sure I know why I'm wrong if I'm wrong because I don't want to give people bad advice.
I appreciate your input always! :slight_smile:


Thanks for your help!


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