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 programming the print function in python

I tried to program a new type of print function, i wanted that the text was printed out in procedural way, so i wrote this:

def print(text):
    for t in text:
        sys.stdout.write(t)
        sys.stdout.flush()
        time.sleep(0.1) 
print('Monkey')
#program stamp  Monkey  letter by letter with delay of 0.1 sec

but if instead of ‘Monkey’ i put inside (‘text’, object.param, ‘text’) he rightly don’t work;
an exemple is:

class Monkey:
     def __init__(self, name):
        self.name=str

Monkey.name = 'George'

def print(text):
    for t in text:
        sys.stdout.write(t)
        sys.stdout.flush()
        time.sleep(0.1)
print('Monkey', Monkey.name, 'is funny')

#TypeError: print() takes 1 positional argument but 2 were given

how i can program this new print for get the same potentiality of the normal print but with the additional of possibility of delay

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

Thank for attention and sorry for the bad english ( pls don’t dv me for that )

>Solution :

Change

def print(text)

to this:

def print(*texts)

It accepts many args more than one.

And do this:

def print(*texts):
    for text in texts:
        for t in text:
           sys.stdout.write(t)
           sys.stdout.flush()
           time.sleep(0.1) 

EDIT

If you want to print out int as well, you can convert int to str:

def print(*texts):
    for text in texts:
        text = str(text) # Notice!! I added this line
        for t in text:
           sys.stdout.write(t)
           sys.stdout.flush()
           time.sleep(0.1) 
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