Problems with Printing While Loop

This is for a really simple project just for fun, called Brendonio’s Big Belly - the point is that it is similar to a clicker, so I have everything in a while loop that loops and reprints.

However, when you run the code, nothing shows up until you press a key, which was not intentionally coded. In addition, after you press the key, it will wait about 1 second and then disappear again.

Does anyone have any possible solutions?

The repl is at this link.

Hello @CCodes!
Instead of using os.system('clear'), use replit.clear or print("\033[H\033[2J", end="", flush=True).
Hope this helps!

1 Like

I think a more clear solution is that you should use print("\033c", end="", flush=True) because os.system("clear") (IIRC) literally prints enough lines to overflow the current text and push it out of the console whereas ansi commands like \033c are used to directly clear the text on the console. Moreoever, through stress tests, the \033c should clear the screen even on replit in a timely manner as long as it’s not a rate more than 5/second :slight_smile: Otherwise if you want to do something more intensive, I recommend self hosting or using a smaller service with less stress on their API

1 Like

Okay, I’ll try that - I will mark as solution if this works. :slight_smile:

1 Like

I think it should work :slight_smile: I’ve fiddled around with ansi quite a bit I even made a working real time updating chat app in the console :slight_smile:

1 Like

Hmm, I tried doing it but it didn’t work. I still have the issue where it requires me to press a key to activate it which is really weird. When I press the key, though, it waits one second and clears without reprinting which is pretty odd

1 Like

Oh I read the code, just delete the line that says time.sleep(1) :slight_smile:

Huh, still doesn’t work. It still does the same thing except it barely shows the text before deleting it and failing to reprint

1 Like

ah you want it to print, you need to move the clear line to directly below the readchar line :slight_smile:

Ah, that works, but now the variables don’t update, it’s basically a static print. Thanks for helping so much btw

1 Like

it should? Can I see the code

Do you want me to invite you?

alright I think that should worrk

Hmm my join link says “post needs to be approved by moderators”

1 Like

oh :(( could you just go to it and input the username “bigminiboss” it happened because back in the day ppl would post join links and random ppl would join

Ok, invited :slight_smile: It should appear in your notifications now

1 Like

Just to let other people who have the same problem know, it eventually did start working, maybe it was a device issue at first :slight_smile:

1 Like

Oof this is really embarrassing but now you have to press a key to iterate the while loop because of the readchar() thing so it’s no longer functional. . .

1 Like

You could also use Threads (example below).

from readchar import readkey
from threading import Thread
import time
currentKey = ""
def myFunction():
   while True:
     print(currentKey)
     time.sleep(1)
Thread(target=myFunction).start()
while True:
  currentKey = readkey()

EDIT: Thanks to Firepup650 for alerting me about the bug

That example would just get stuck in a loop of printing nothing.

1 Like