0

I'll start by saying that I did look up answers to this question and unfortunately, I just couldn't understand them or they didn't seem to work for me. This is of course down to me rather than the people who answered the question, so I do apologise in advance.

So pretty much I'm trying to call a variable that is assigned by the user from one function to the other, I'll give an example:

def function_one():
    a = input("What is your name?")
    return a
def function_two():
    print("Nice to meet you, "a)

function_one()
function_two()

This of course does not work and I'm sure that is down to my own stupidity, I wasn't sure why at first because I saw other people saying to simply return the variable, which I did!

I also tried calling the variable from the other function, for example:

def function_two()
    a = function_one()

but I realised that was pretty stupid since I'm just assigning that function as a, so it's not going to work.

I'd appreciate some insight, I know these are not the kind of questions you'd expect but yeah... I'm clueless.

3
  • 2
    "but I realised [...], so it's not going to work." But did you actually run it and see whether it worked? If not, I suggest trying that now ;-) Commented Mar 29, 2017 at 19:05
  • You do have a problem in your second print statement. Since you didn't identify the error you're getting, it's hard to tell if that's a transcription error or part of the actual problem. Commented Mar 29, 2017 at 19:08
  • second function (calling function inside of second function) will work and it is not so bad. For why 1st one is not working: it returns input, but you don't save that input, you must assign it to some variable: a = function_one(). For second function, you need to pass that value stored in a to the function or use global a to use global variable. To pass the function: def function_two(a) will mean it will get variable a as parameter and call it as function_two(a) will give the result. You can also do something like function_two(function_one()) Commented Mar 29, 2017 at 19:10

3 Answers 3

2

I think what you want to do is take user input, store it in a variable, then greet the user using that variable:

def ask_for_users_name():
    name = input("What is your name?")
    return name
def greet_user(name):
    print("Nice to meet you, " + name)

users_name = ask_for_users_name()
greet_user(users_name)

One important thing to note is that I had to concatenated the name with the string "Nice to meet you, " using the + operator.

Edit:

To answer to the question in the comments, you could do something like this in that case:

def ask_for_user_info():
    name = input("What is your name?")
    age = input("What is your age?")
    return name, age

user_name, user_age = ask_for_user_info()

Best practice is to make functions that only do one thing, for many reasons, one is that the name of the function normally replaces any need for inline comments:

def ask_for_user_name():
    name = input("What is your name?")
    return name
def ask_for_user_age():
    age = input("What is your age?")
    return age

In the case of the ask_for_user_info() method, it isn't immediately clear what exactly it is doing from the name.

Edit 2:

You could then use those two functions like this, in either order:

age = ask_for_user_age()
name = ask_for_user_name()

or

name = ask_for_user_name()

or

age = ask_for_user_name()
Sign up to request clarification or add additional context in comments.

7 Comments

Ahh, that makes sense. But what if inside the function 'ask_for_users_name' there was also other things happening (I know it's probably bad practice but theoretically), if it asked you for your age in the first function etc, won't that be repeated when you assign the variable users_name to that function?
Hey good question, I'll edit the original post to answer your question. I cant really post code well in comments
Sorry about this, I appreciate the constant help but I have another question. I understand that you want to call both, but what if you had both name AND age, but you only wanted to call age, and not name? Then how would it work? Because by what I'm seeing here I wouldn't be able to call age without calling name too, is that the case?
No worries whats up!
Edited my comment!
|
0

you do have it. this should work.

def function_two():
    a = function_one()
    print('hi {}'.format(a))

then

>>>function_two()

Comments

0

Another way is to use this:

 def ask_for_users_name():
      name = input("What is your name?")
      greet_user(name)

 def greet_user(user_name):
     print("Nice to meet you,"+user_name)

 ask_for_users_name()

You merely call ask_for_users_name() at the end.

Edit:

greet_user_() is a void function, which means it does not return anything. In this case, all it does is receive input passed to it and prints it. If you want to perform other operations, you can pass it additional parameters, or keep it the way it is.

Version 1:

 def ask_for_users_name():
     name = input("What is your name?")
     age = int(input("What is your age? "))
     print("Your age is ", age)
     greet_user(name)

 def greet_user(user_name):
     print("Nice to meet you,"+user_name)

 ask_for_users_name()

In version 1, we are still utilizing greet_user() to just print one thing, and performing another print operation in ask_for_users_name().

Version 2:

def ask_for_users_name():
     name = input("What is your name?")
     age = int(input("What is your age? "))
     greet_user(name, age)

def greet_user(user_name, user_age):
     print("Nice to meet you,"+user_name)
     print("Your age is", user_age)

ask_for_users_name()

In version 2, we are passing both age and name to greet_user(), which in turn prints out the passed variables. I hope this helps.

1 Comment

Thank you for the answer! However what if the ask_for_users_name() function also included other parts, say it was a function that also asked for your age (I know it's bad practice) and the only thing I wanted from the function was the name, what then?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.