Answer the question
In order to leave comments, you need to log in
[Python] How to move a line?
Greetings, it is necessary to improve the code, namely, to make the line that will be given move to the right (as in a running line), and at the end, on the right side, the characters slowly disappear one by one.
import time
stroka = input("")
for slovo in stroka:
print(slovo,end=" ", flush=True)
time.sleep(1) # Время в мс
Answer the question
In order to leave comments, you need to log in
It is not entirely clear how it is supposed to move to the right, usually the scrolling line moves to the left. But if you wish, you can change the display, if you really need it in the opposite direction.
import time
stroka = input("")
num_chars = 5 # размер окна символов
for i in range(len(stroka)-num_chars+1):
print(stroka[i:i+num_chars],end="\r", flush=True)
time.sleep(1) # Время в мс
For the sake of clarity, here's what I can suggest:
import sys
from time import sleep
def cut_frame(s: str, start_pos: int = 0, frame_size: int = None) -> str:
frame_size = frame_size or len(s)
left_space = ' ' * min(start_pos, frame_size)
if start_pos < 0:
s = s[-start_pos:]
return f'{(left_space + s)[:frame_size]:{frame_size}}'
def scroll(s: str, frame_size: int = None, timeout: float = 0.1, template: str = '[{}]', file=sys.stdout):
frame_size = frame_size or min(len(s), 80)
for i in range(-len(s), frame_size + 1)[::-1]:
substring = template.format(cut_frame(s, i, frame_size))
print(substring, end='\033[0G', flush=True, file=file)
sleep(timeout)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question