I am a beginner and tried to make a code such that it appears letter by and letter and also accepts an input, but None appears

Viewed 15
import sys
import time

def delay_print(s):
    for c in s:
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(0.01)

Colour = input(delay_print("Choose a colour(Red/Black):"))

Expected_result:

Choose a colour(Red/Black):

Actual_result

Choose a colour(Red/Black):None

2 Answers

You don't need to provide the return value (None) from delay_print() as the parameter for input(). Just split it into two lines:

delay_print("Choose a colour(Red/Black):")
colour = input()

If you arrange for delay_print to return an empty string then this will do what you need:

from time import sleep

def delay_prompt(s):
    for c in s:
        print(c, end='', flush=True)
        sleep(0.25)
    return ''

value = input(delay_prompt('Hello world: '))
Related