Почему мой код печатает несколько одинаковых строк, когда достигает конца терминала?Python

Программы на Python
Anonymous
Почему мой код печатает несколько одинаковых строк, когда достигает конца терминала?

Сообщение Anonymous »

Код: Выделить всё

import time
import sys

def animate_string(input_string, delay_second=0.01):
current_string = ""
for target_char in input_string:
if target_char == '\n':
# Print a newline and reset current_string
print()
current_string = ""
continue

current_char = 'a'
while current_char != target_char:
a_code = ord(current_char)
# previous string + current letter
sys.stdout.write('\r' + current_string + current_char)
sys.stdout.flush()
time.sleep(delay_second)
# Move to the next char (used ASCII)
current_char = chr(ord(current_char) + 1)
# Handle bound issue
if current_char == '{':  # After 'z'
current_char = 'A'
elif current_char == '[':  # After 'Z'
current_char = target_char
# Add the target character to the current string
current_string += target_char
# Print the updated current string
sys.stdout.write('\r' + current_string)
sys.stdout.flush()
# Print a newline at the end to finish the output cleanly
print()

animate_string("fjweiofjewoifjweofjewofjewffewjfiowjfwaifjewipfajeehgpwth235r2fjweiofjewoifjweofjewofjewffewjfiowjfwaifjewipfajeehgpwth235r2fjweiofjewoifjweofjewofjewffewjfiowjfwaifjewipfajeehgpwth235r2fjweiofjewoifjweofjewofjewffewjfiowjfwaifjewipfajeehgpwth235r2", 0.0002)
Это мой код. Я классно работаю над печатью строк. Он отлично работает с небольшим количеством символов, но когда строка становится достаточно длинной, чтобы достичь конца терминала, он начинает печатать одну и ту же строку снова и снова. почему это происходит и как я могу решить эту проблему?

Подробнее здесь: https://stackoverflow.com/questions/787 ... f-terminal

Вернуться в «Python»