Printing user input?

M

Mohammed_M

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
 
K

kyosohma

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

It's a scope issue. You have the variable, "name", inside a function
and then you try to reference it outside the function, where it is not
defined. Thus, Python gives an error. To fix this, put the print
statement in the function itself...and when you use print, you don't
usually put the object to be printed in parentheses.

def PersonsDetails():
name = input("What's your name?")
print name
PersonsDetails()

See also this article on scopes and namespaces:

http://www.network-theory.co.uk/docs/pytut/PythonScopesandNameSpaces.html
or
http://www.diveintopython.org/html_processing/locals_and_globals.html

Hope that helps some.

Mike
 
L

Lorenzo E. Danielsson

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
 
J

J. Clifford Dyer

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

Also, you'll want to use raw_input() instead of input.

Cheers,
Cliff
 
M

Mohammed_M

Thanks Mike, Lorenzo & Cliff for your replies.
I definately will be reading up on namespaces & scopes.
Thanks again guys :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top