3. while you're at it

says I should check for infinite loops?!?!

Hi, @cinpacheco22 ,

In your code, this statement is not indented, so it is not part of the while loop …

num+=1

Therefore, num is always equal to 1, which was originally assigned to it, and the loop never terminates.

Indent the statement to make it part of the loop.

1 Like
num = 1

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

This should work

2 Likes

also this another approach:

num = 1

while num < 11:
print(num**2)
num += 1

1 Like

Why parentheses here in print?

If you are running new version of Python, then you would need to use print statement with parenthesis as in print (“Hello, Python!”);.

http://www.tutorialspoint.com/python/python_basic_syntax.htm

I don’t have to use parentheses… Is there something wrong with that? :frowning:

Can anyone Explain this?

@lmuellercolostate: Instead of doing num += num
You should’ve done

num += 1

There’s a huge difference betweent those two.
In the first one, you’ll always get a number equal to two times num, i.e.:

num = 1 print num #1 num += num # 1 + 1 print num #2 num += num # 2 + 2 print num# 4
While on the second one, you’ll be adding your variable num by a constant, in this case 1.

1 Like