Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Why doesnt my while loop work?

countdown = 3
while countdown:
    print(countdown)
countdown -= 1

if countdown = 1:
    print("yay")
Steven Parker
Steven Parker
243,656 Points

I would guess that you might have an indentation error, but it's impossible to tell or give a specific answer with unformatted code.

Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Edited my indentation... is that better? Didn't work... got this Error Message

countdown = 3
while countdown:
... print(countdown)
... countdown -= 1
File "<stdin>", line 3
countdown -= 1
^
SyntaxError: invalid syntax
if countdown = 1:
File "<stdin>", line 1
if countdown = 1:
^
SyntaxError: invalid syntax
print("yay")

1 Answer

Steven Parker
Steven Parker
243,656 Points

Only the first print statement is inside the loop. Since the line after that is not indented is signifies the end of the loop. I'd guess you intended for all the lines below to be inside the loop, so they all need to be moved to the right one stop.

Also the "if" should have a comparison ("==") instead of an assignment ("=").