Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Hrithik Sharma
Hrithik Sharma
6,090 Points

Why the while loop is not working in python?

Function alone works fine but when put it in while it doesn't work do anyone know What's the problem Code:-

import webbrowser 
import time
i = 0
def startBrowser(seconds, url):
    time.sleep(seconds)
    webbrowser.open_new(url)

while i == 2:
    startBrowser(10, "www.google.com")
    i += 1

1 Answer

Steven Parker
Steven Parker
215,960 Points

The "while" condition is checking to see if "i" is equal to 2. Since "i" starts out set to 0, this condition is not true so the loop never runs.

Perhaps you meant to write: "while i < 2:" instead?

Hrithik Sharma
Hrithik Sharma
6,090 Points

i figured that out as well how stupid of me

Steven Parker
Steven Parker
215,960 Points

Mistakes are never "stupid", they're just "learning opportuniities". :wink: