0

I make stop-motion animation films, and I wanted to see if I could do something like that with coding. I have this:

import sleep
print('   _  ')
print('  |_| ')
print('  -|-  ')
print('   |  ')
print('  /\  ')
time.sleep(.1)
print('   _  ')
print('  |_| ')
print('  -|/  ')
print('   |  ')
print('  /\  ')

So essentially, it will look like the stick figure is waving. But, this of course only prints out the 2nd stick figure below it. I am wondering how I can make it delete the first one, then replace it with the second one.

4 Answers4

0
os.system('cls||clear')

Sleeping 1 or 2 seconds before clearing will be better.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
KPacaman
  • 1
  • 1
  • 1
    Welcome to Stack Overflow! Please take the [tour] and read [answer]. Formatting help for SO's flavor of markdown is [here](/help/formatting). Remember that the answers you write will remain here for people of the future to read if they encounter the same problem. Keeping this in mind, it's _always_ better to include all relevant details in your answer instead of posting a brief, code-only answer or one that contains unjustified statements, such as _"Sleeping 1 or 2 seconds before clearing..."_: why would this be better? – Pranav Hosangadi Oct 27 '20 at 19:44
0

In the case of dynamic printouts / displays, I would suggest using IPython's display module.

Note: I've edited my answer (after seeing many of the responses here) to allow for terminal as well as notebook display choices...

# Create your figures
fig_1 = """
   _
  |_|
  -|-
   |
  /\\
"""

fig_2 = """
   _
  |_|
  -|/
   |
  /\\
"""


# Now the code / display
from IPython.display import display, clear_output
import time
import os

notebook = False
display(fig_1)
time.sleep(.1)
if notebook: clear_output(wait=True)
else: os.system('clear')
display(fig_2)     

A fancier way of doing this:

# A fancier way of doing it
def display_animated_figs(all_figs:list, sleep_s:float, notebook=False):
    """Displays each figure in the list of figs,
    while waiting between to give `animated feel`

    notebook: (bool) are you operating in a notebook? (True) Terminal? (False)
    """
    
    for i, fig in enumerate(all_figs):
        # Always clear at start...
        # Allow for notebooks or terminal
        if notebook:
            clear_output(wait=True)
        else:
            os.system('clear')
        # After the first figure, wait
        if i>0:
            time.sleep(sleep_s)
            
        display(fig)
   
   # All done, nothing to return  

# Now execute
my_figs = [fig_1, fig_2]
display_animated_figs(my_figs, 0.1, False)
Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69
0

on linux you want os.system('clear')

on windows you want os.system('CLS')

if you mean to clear the idle window, that's not possible without some sort of plugin.

user3150635
  • 509
  • 2
  • 9
  • 26
0

I have 3 methods:

method 1. print empty lines. this code just prints '' 50 times so anything printed earlier will go off the screen

[print('') for x in range(50)]

This is a list comprehension, basically the same thing as a for loop.

method 2. prints the newline char (an enter) * 50, that's just 50 new lines

print('\n' * 50)

(yes you can do * and + on strings with python. if you are planning on doing more with python I would highly recommend looking into that)

method 3. cls command. this will call the cls (or clear screen) command. you do have to import os though.

os.system('cls')

this uses the system function from the os library. this function can do many useful things, one is clearing the screen

Jorrit200
  • 23
  • 2
  • 8