Caden
Caden

Reputation: 3

How to print strings from a defined function

I am wondering how I can print "myName, myAge, myHeight, myJob" from the function me(). It appears that I can't have it in the function and it gives me an error saying myName, etc. is not defined... How exactly can I fix this? I need it in this format, because of what I am planning on doing. If you'd like my full project code, please let me know.

def me():
myName = raw_input("What is your name? ")
myAge = int(raw_input("How old are you? "))
myHeight = int(raw_input("How tall are you? "))
myJob = raw_input("What is your job? ")

me()
print myName
print myAge
print myHeight
print myJob

Upvotes: 0

Views: 79

Answers (3)

tripleee
tripleee

Reputation: 189327

You cannot easily access the internals of a function. Part of the reason we use functions is that they encapsulate their behavior, so changes elsewhere in your program cannot affect what happens inside of your function.

The usual way to approach this would be for the me function to return the values the user provides, perhaps as a dict.

def me():
    info = dict()
    info['name'] = raw_input("What is your name? ")
    info['age'] = int(raw_input("How old are you? "))
    info['height'] = int(raw_input("How tall are you? "))
    info['job'] = raw_input("What is your job? ")
    return info

caden = me()
for key in caden:  # notice how this will order the fields randomly
    print('{0}: {1}'.format(key, caden[key]))

This is sort of "inside out"; for real programs, it often makes sense to have the interactions in the outer layers and the logic parts inside functions (because the I/O flows tend to change between programs, whereas the business logic is code you want to be able to import and reuse).

Upvotes: 0

Delioth
Delioth

Reputation: 1554

So, this is a question relating to scope. Scope is an idea that we want to encapsulate variables in a small little chunk, rather than letting anyone anywhere modify and use them. When you run a function, you create a closure - a new scope. Anything defined in that scope is only viable in that scope, unless you explicitly allow it to leave that scope.

There are two ways to let things leave scope- you can make the variables global (typically bad practice, since it can lead to weird bugs if you aren't careful) or you can return variables.

To return them, you just do exactly that- return is a statement just like print, except return immediately ends any function when it is hit (it's saying "This is what you wanted from the function, I'm done now"). You can return multiple things, typically as a tuple or a list.

def me():
    # get your inputs
    ...
    return myName, myAge, myHeight, myJob

And on the other end, you have to receive them

info_tuple = me()

info_tuple is now the data you collected- you can print just like you would, or you could print them one by one

print info_tuple
# OR
for info in info_tuple:
    print info

Additionally, you don't have to catch the whole returned value as one tuple- python can unpack them for you:

myName, myAge, myHeight, myJob = me()

Will give you the same things, just named according to each rather than just the four ordered pieces of the tuple.

Upvotes: 1

Hani
Hani

Reputation: 1424

this should work

the variables that are defined in the function are not visible outside it so you just have to print them while you are in the function not outside

def me():
  myName = raw_input("What is your name? ")
  myAge = int(raw_input("How old are you? "))
  myHeight = int(raw_input("How tall are you? "))
  myJob = raw_input("What is your job? ")
  print myName
  print myAge
  print myHeight
  print myJob

me()

Upvotes: 0

Related Questions