By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Printing user input?

P: n/a
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
Nov 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 15, 11:03 am, Mohammed_M <Moh...@gmail.comwrote:
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...ameSpaces.html
or
http://www.diveintopython.org/html_p...d_globals.html

Hope that helps some.

Mike
Nov 15 '07 #2

P: n/a

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

Nov 15 '07 #3

P: n/a
On Thu, Nov 15, 2007 at 09:03:26AM -0800, Mohammed_M wrote regarding Printing user input?:
>
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
--
http://mail.python.org/mailman/listinfo/python-list
Also, you'll want to use raw_input() instead of input.

Cheers,
Cliff
Nov 15 '07 #4

P: n/a
Thanks Mike, Lorenzo & Cliff for your replies.
I definately will be reading up on namespaces & scopes.
Thanks again guys :)
Nov 15 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.