0

I am testing a print function, where it looks as if the prompt is typing, defined as type()

I would like to store raw input using the type function:

from time import sleep
import sys
from random import uniform

def type(s):
    for c in s:
    sys.stdout.write('%s' % c)
    sys.stdout.flush()
    sleep(uniform(0, 0.3))

name = raw_input(type("What is your name? "))
type("Hello " + name +"\n")

This is the output of the code:

What is your name? None

Input is still allowed from the user, right after 'None', and the output will be correctly printed, without 'None'. Is there a way to circumvent this?

In this prompt I would like to print everything using the type function.

2
  • The problem is that you are passing type() to raw_input. type implicitely returns None, since you did not give it an explicit return value. Perhaps try taking raw_input inside type and returning the result. As an aside, the name type already belongs to the built-in function type, and it is bad practice to shadow it. Choose a different name for your function rather than type. Commented Jan 13, 2017 at 6:55
  • Python already has a function type(). Rename yours to type_() or something else Commented Jan 13, 2017 at 6:56

3 Answers 3

5

raw_input turns the arg you pass it into a string & uses that as the prompt. You're passing raw_input the return value of your type function, and that function returns the default of None, so that's why "None" gets printed. So just use your function before calling raw_input and call raw_input without an arg.

BTW, you should not use type as a variable or function name because that's the name of a built-in function.

from time import sleep
import sys
from random import uniform

def typer(s):
    for c in s:
        sys.stdout.write('%s' % c)
        sys.stdout.flush()
        sleep(uniform(0, 0.3))

typer("What is your name? ")
name = raw_input()
typer("Hello " + name + "\n")
Sign up to request clarification or add additional context in comments.

Comments

1
type("What is your name? ")
name = raw_input()
type("Hello " + name +"\n")

There you go. Any function in Python which has no return statement defined, returns a None by default - which was showing up on your prompt.

3 Comments

name = raw_input("What is your name? ") type("Hello " + name +"\n")
@ASR putting multiline Python code into comments is useless because the indentation gets lost.
I honestly have no idea why I was trying to complicate this. Thanks for your help. For some reason, I thought that the input would start on a new line.
0

The default return type for a function is None. Thus what is happening is that your type function returns at the end of the function with a None which the raw_input then puts into the stdout stream. Simply having the type function return a blank string would handle the issue:

    return ''

You might want to use a different function name other then type because type is a builtin and can be useful sometimes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.