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

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
229,744 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
229,744 Points

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