Reputation: 42
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.
Upvotes: 0
Views: 183
Reputation: 55489
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")
Upvotes: 5
Reputation: 186
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.
Upvotes: 0
Reputation: 1161
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.
Upvotes: 1