ZaruStarru
ZaruStarru

Reputation: 21

os.system('cls') opens up command prompt instead of clearing the shell?

I'm working on a text adventure game using Python that has character-by-character scrolling text. So, I made this to try and give off the illusion of adding one character every loop, and deleting the last, less complete string. However, instead of deleting the last screen, it opens up command prompt.

I've tried using print('n\ * 100') and other such printing tricks, but it doesn't have the result I'm looking for. I've also tried switching between 'cls' and 'clear' in case I've been doing it wrong, but even when I made an if statement citing a difference between the two I'm still getting errors.

while charcount < len(text):

if os.name == 'nt':

  os.system('cls')

else:

  os.system('clear')

newtext.append(text[charcount])

print("".join(newtext))

charcount += 1

I expected the screen to clear and only print newtext's current iteration, but instead it ignores this, does not clear the screen, and repeatedly opens and closes command prompt.

Upvotes: 2

Views: 235

Answers (1)

Akash Govind
Akash Govind

Reputation: 21

Your code works fine in CMD and PowerShell.
That makes me assume that you are using IDLE because the behavior you stated is reproducible only when I use IDLE.

If that's the case, it's not possible to clear IDLE window. You can still use CMD and it would work just fine.

Refer Any way to clear python's IDLE window?

Upvotes: 2

Related Questions