Search code examples
pythonvarraw-inputfunction

Random None when printing from raw_input


I am new to coding I recently made a Decimal to Binary and Binary to Decimal Translator and I am wanting the text for the raw_input to type out one letter at a time like a typewriter, but I am getting after a None after it is done because function is not returning anything. I am not sure how to fix this because I want the raw_input to be a variable and trying to return things gives me syntax errors. Any help appreciated.

import time
import sys
def type(string):
    for x in string:
        sys.stdout.write(x)
        sys.stdout.flush()
        time.sleep(.01)

dbbd = raw_input(type('Type "db" for decimal to binary, Type "bd" for binary to decimal.'))

here is the important part^

full program for anyone interested: http://pastebin.com/GPw81Mi5


Solution

  • raw_input expects a string as input

    >>> help(raw_input)
    

    What you could do is the first print your prompt string and then ask for the input from the user.

    import time
    import sys
    def typewriter(string):
        for x in string:
            sys.stdout.write(x)
            sys.stdout.flush()
            time.sleep(.01)
    typewriter('Type "db" for decimal to binary, Type "bd" for binary to decimal: ')
    dbbd = raw_input()