Printing user input?

Lorenzo E. Danielsson danielsson.lorenzo at gmail.com
Thu Nov 15 12:36:33 EST 2007


On Thu, 2007-11-15 at 09:03 -0800, Mohammed_M wrote:
> Hi,
> I'm v.new to Python, so please don't be too harsh :)
> I get a NameError with the code below - All I want to do is store some
> input taken from the user in a variable called name, & then print name
> 
> # START CODE ==========================================
> # Print name demo
> 
> 
> def PersonsDetails():
>     name = input("What's your name?")
> PersonsDetails()
> 
> print(name)
> 
> 
> # END CODE ==========================================
> 
> Thanks for reading & any help on this

You will need to return the input from the function.

def PersonsDetails():
	return raw_input("What is your name? ")

name = PersonsDetails()
print name

Notice that in your code the variable name is created inside the
function PersonsDetails(), so it is scoped to the function. This means
that the variable is not accessible outside of PersonsDetails() and the
attempt to print it with print(name) will fail.

Lorenzo




More information about the Python-list mailing list