while True
I am new on Python.
When I do "while True" it goes wrong and stops. Why?
Exampel:
while True:
print("Hello")
when you do a while True loop without any interval, the programs overloads and "forgets" to take the time to put it on screen.
Try indenting, because this should work, maybe your repl is down, so try on IDLE.
It prints hello
every possible time it can, no interval, no stopping. Use the time
module to add a pause.
from time import sleep interval = <TIME IN SECONDS YOU WANT TO INTERVAL> while True: print("Hello") sleep(interval)
Of course, you put your interval into the interval
variable.
Makes sense?
@Lumpy_Wiggles This is python, you don't do that.
@HarveyH Don't do what?
@HarveyH You're wrong, You can in fact pass an integer to a function as long as it's an integer sleep() can still use it
@Lumpy_Wiggles I ment to say that there is a simpler way. wich is:
import time
while True:
print("Hello")
time.sleep()
Sorry for any confusion...
Yeah it breaks because it's printing hello every single possible frame, overloads it.
It's printing "Hello" forever until your browser crashes. What do you want it to do?
@timmy_i_chen
I would like to test while True and se how it prints "Hello" over and over again but it dosen´t print it at all. And I have got a message (not from Python) on the screen that something is wrong. I have to restart everything. Is my computer overloaded?
@AgnetaLarsson Can you send a screenshot of what you're seeing? This is what I get:
This file cannot be displayed: https://storage.googleapis.com/replit/images/1533062162265_20106537e991f7b2dfa21727154d93ee.pn
@timmy_i_chen
It works! Thank you!
This file cannot be displayed: https://storage.googleapis.com/replit/images/1533069923557_df77e7ccba6a7154c862f5c2d4e0209f.pn
Your overloading it. It's printing Hello every possible frames and therefore crashes it. Try this instead:
By doing that, it'll wait 5 seconds before print Hello.
@HarveyH I had the same problem when I was starting python. Also, if you are only using sleep the instead of import time, put from time import sleep for good practice.
@JakobNacanaynay Yeah!
That works too!
And this @HarveyH