Skip to content
← Back to Community
while True
Profile icon
AgnetaLarsson

I am new on Python.
When I do "while True" it goes wrong and stops. Why?
Exampel:
while True:
print("Hello")

Voters
Profile icon
epeefencer
Profile icon
Wilke000
Profile icon
The_Potato
Profile icon
HarveyH
Profile icon
sandrew588
Profile icon
AgnetaLarsson
Comments
hotnewtop
Profile icon
HarveyH

Your overloading it. It's printing Hello every possible frames and therefore crashes it. Try this instead:

import time while True: time.sleep(5) print("Hello")

By doing that, it'll wait 5 seconds before print Hello.

Profile icon
JakobNacanaynay

@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.

Profile icon
HarveyH

@JakobNacanaynay Yeah!

from time import sleep while True: sleep(5) print("Hello")

That works too!

Profile icon
Wilke000

And this @HarveyH

from time import sleep as slp while True: slp(5) print("Hello")
Profile icon
JakobNacanaynay

when you do a while True loop without any interval, the programs overloads and "forgets" to take the time to put it on screen.

Profile icon
RyanRana

Try indenting, because this should work, maybe your repl is down, so try on IDLE.

Profile icon
Lumpy_Wiggles

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?

Profile icon
HarveyH

@Lumpy_Wiggles This is python, you don't do that.

Profile icon
Lumpy_Wiggles

@HarveyH Don't do what?

Profile icon
k4t3xpl0si0n

@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

Profile icon
HarveyH

@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...

Profile icon
demonismog

Yeah it breaks because it's printing hello every single possible frame, overloads it.

Profile icon
timmy_i_chen

It's printing "Hello" forever until your browser crashes. What do you want it to do?

Profile icon
AgnetaLarsson

@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?

Profile icon
timmy_i_chen

@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

Profile icon
AgnetaLarsson

@timmy_i_chen
It works! Thank you!
This file cannot be displayed: https://storage.googleapis.com/replit/images/1533069923557_df77e7ccba6a7154c862f5c2d4e0209f.pn