Ask coding questions

← Back to all posts
while True
AgnetaLarsson

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

Voters
epeefencer
Wilke000
The_Potato
HarveyH
sandrew588
AgnetaLarsson
Comments
hotnewtop
HarveyH

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.

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.

HarveyH

@JakobNacanaynay Yeah!

That works too!

Wilke000

And this @HarveyH

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.

RyanRana

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

Lumpy_Wiggles

It prints hello every possible time it can, no interval, no stopping. Use the time module to add a pause.

Of course, you put your interval into the interval variable.

Makes sense?

HarveyH

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

Lumpy_Wiggles

@HarveyH Don't do what?

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

HarveyH

@Lumpy_Wiggles I ment to say that there is a simpler way. wich is:

import time

while True:
print("Hello")
time.sleep(<number you want to wait>)

Sorry for any confusion...

demonismog

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

timmy_i_chen

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

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?

timmy_i_chen

@AgnetaLarsson Can you send a screenshot of what you're seeing? This is what I get:

https://storage.googleapis.com/replit/images/1533062162265_20106537e991f7b2dfa21727154d93ee.pn

AgnetaLarsson

@timmy_i_chen
It works! Thank you!
https://storage.googleapis.com/replit/images/1533069923557_df77e7ccba6a7154c862f5c2d4e0209f.pn