Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I add multiple string arguments to my typing function in Python?

I made a type() function which functions similarly to print except it types character by character. The problem is, when I try to implement multiple str arguments it just prints everything at once.

The code I used originally was:

import sys
import time
def type(text):
  for char in text:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.05)
  print()

In order to add more str arguments, I did:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import sys
import time
def type(*text):
  for char in text:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.05)
  print()

In practice, however, it usually just prints everything at once, like a normal print function.
Any pointers?

>Solution :

If text is a list, and the length of it varies, you can do:

def type(text):
for string in text:
    for char in string:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.05)
  print()

So you loop through the lists for each string, and for each string you loop through all the characters.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading