<code>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)
</code>
<code>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)
</code>
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)
This is my code. I am working on printing string in a cool way. It works fine with small number of characters, but when the string becomes long enough to reach the end of terminal, it starts printing the same line over and over. why does this happen and how can I solve the issue?
New contributor
bluepiplup is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.