0

It only works visually in PyCharm

I'm writing an Elevator program and I tried to do the waiting lights animation like a real elevator. But I would like to print some outputs after, so that you can see the entire elevator before the lights animation starts.

from termcolor import cprint
import time

# this would be user's input
level = 10

# ABOVE PART ELEVATOR
cprint('         ▲  ▼        ', 'yellow')
cprint('    ┏' + ('━' * 13) + '┓')

# LIGHTS
print('    ┃', end='')
for lights in range(1, 11):
    cprint('⦿', 'yellow', end='', flush=True)
    time.sleep(0.5)
print('┃')

# LOWER PART ELEVATOR
print('    ┣━━━━━━╥━━━━━━┫')
print('    ┃      ║      ┃\n' * 5 + '    ┃      ║      ┃')
print('━━━━┗━━━━━━╨━━━━━━┛━━━━')

print(f'\nYou have arrived at floor ', end='')
cprint(level, 'yellow')

So this is the program that prints from left to right and from up to down like a normal program. But I would like the output to be:

         ▲  ▼        
    ┏━━━━━━━━━━━━━┓
    ┃   #lights   ┃ <- animation appears after the entire elevator
    ┣━━━━━━╥━━━━━━┫
    ┃      ║      ┃
    ┃      ║      ┃
    ┃      ║      ┃
    ┃      ║      ┃
    ┃      ║      ┃
    ┃      ║      ┃
━━━━┗━━━━━━╨━━━━━━┛━━━━

"You have arrived at floor 10" <- this after lights

2 Answers2

1

The problem with your code was that the lower part of your elevator only got drawn after conclusion of the for loop that displays the lights. You have to draw the entire elevator within the for loop, this way the elevator will show while the lights are updating. Note: To do this nicely you should always clear the console after every iteration of the for loop. To do this you have to do from os import system and to then clear your screen you do system("clear") on Linux or Mac and system("cls") on Windows. Here is the edited code:

from termcolor import cprint
import time
from os import system

level = int(input('Which floor would you like to visit?\n')) + 1
light = '⦿'
lights = light

for currentlevel in range(1, level):
    system('clear') 
    cprint('         ▲  ▼        ', 'yellow')
    cprint('    ┏' + ('━' * 13) + '┓')
    print('    ┃', end='')
    cprint(lights, 'yellow', end='', flush=True)
    print('┃')
    print('    ┣━━━━━━╥━━━━━━┫')
    print('    ┃      ║      ┃\n' * 5 + '    ┃      ║      ┃')
    print('━━━━┗━━━━━━╨━━━━━━┛━━━━')
    print(f'\nYou have arrived at floor ', end='')
    cprint(currentlevel, 'yellow')
    time.sleep(0.5)
    lights += light

Cheers!

LeSchlongLong
  • 350
  • 2
  • 8
0

Please refer this to manipulate multiple lines in screen. And be aware that it only works for native windows console but not workable in pycharm console which printed error as "Redirection is not supported."

Wilson.F
  • 49
  • 3
  • I tried it and doesn't work with the same code of the link. The error is: `Traceback (most recent call last): File "C:\Users\xxxxx\Desktop\xxxxx\xxxx\test.py", line 1, in import curses File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python38-32\lib\curses\__init__.py", line 13, in from _curses import * ModuleNotFoundError: No module named '_curses'` – Ludovico Zocchi Oct 22 '20 at 10:29
  • I've set up my curses package via this command "pip install windows-curses" and it works. – Wilson.F Oct 23 '20 at 01:38
  • On my environment, "windows-curse" installed "_curses.cp37-win_amd64.pyd" which contains module _curses, if deleted this file, the program just complain the same error as yours. – Wilson.F Oct 23 '20 at 01:48