1

so I've started learning python again and I'm currently making a mini movie-recommendator. I want my code to be a little bit more understandable so I'm always trying to use def to make the code simple. My problem is ;

def welcome():
    print("""Welcome to the low budget film recommender!
             Here you can tell me what kind of movies do you like or what movie did you watch
             and I'll suggest you a movie from my database according to that.""")
    name = input("But first I need to learn your name:>> ").capitalize()
    print(f"Nice to meet you {name}")
    return name

I want to use the name variable outside of the function(inside another function actually) but it gives me NameError and says "name" is not defined. How can I fix this and use the name variable outside of the function?

3 Answers3

3

It's not best-practice to declare it as a global variable (as the other answers have recommended).

You should go to wherever welcome() is called and set a variable with the result (which you are returning inside welcome):

name = welcome()
print(f"This is the result of welcome: {name}")
Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
  • Okay this works too but I didn't understand why making it a global variable is not useful, and also could you please explain what this code is supposed to do? It works but i didn't get it – Ersin Burak Apr 06 '20 at 20:30
  • This is by far the best way to do out of the current 3 answers. Your function welcome() returns the name it got, and that can be stored in any (local) variable, which here happens to also be called 'name' but could get any other meaningful name like 'user_name'. Using globals make (long) code harder to read and debug, as the value can change "anywhere"/"anytime". Ans harder to maintain as the variable name has to stay the same in the function and where it is used. And the function cannot be called twice for different things, and so on – B. Go Apr 06 '20 at 21:08
  • As previously discussed here, there is no problem with using global: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function?rq=1 – Mehrdad Salimi Apr 06 '20 at 21:11
  • @B.Go oh okay now I think I got it, so we're actually storing the name value that we returned from the welcome() function and we can store it something like user_name = welcome(). so the whole stored welcome() thing is actually our returned name value. Am I right? – Ersin Burak Apr 06 '20 at 21:18
  • You store whatever the 'return' line(s) of your function gives (usually at the end of your function, but you can have several times that keywords, for example in if and in else). Into whatever (new) variable you want when calling the function. Which allows to easily call the function several times and welcome several peoples to say user1, user2... – B. Go Apr 06 '20 at 21:22
  • 1
    @MehrdadSalimi There is actually a problem with using global. If you're calling a function that retrieves something and returns that thing, you're implying that the function doesn't change any internal state. Changing a global variable AND returning the value is counter-intuitive and counts as code smell because that sort of pattern is extremely likely to introduce unforseen bugs. That's why you'd (hopefully) never see something like that in a well organized project. – Christian Stewart Apr 07 '20 at 21:33
  • @ChristianStewart Got it. Thanks. – Mehrdad Salimi Apr 09 '20 at 09:02
-1

I have modified your code a bit and by using the name variable as a global variable you can achieve what you want. Have a look at the code

#name as a global variable 
name = input("But first I need to learn your name:>> ").capitalize()

def welcome():
    print("""Welcome to the low budget film recommender!
             Here you can tell me what kind of movies do you like or what movie did you watch
             and I'll suggest you a movie from my database according to that.""")
    # name = input("But first I need to learn your name:>> ").capitalize()
    print(f"Nice to meet you {name}")
    return name

print(welcome())

def message():
    return name

print(message())

Let me know if you still need any assistance

  • I think I got it, Can I ask you why are we using "return" here, I tried to code without the return and it still worked without any problem actually. Do we really have to return the name variable if we use the code like this? – Ersin Burak Apr 06 '20 at 19:32
  • And I also used it as welcome() rather than print(welcome()), what is the difference there? Sorry for all my questions, It's been a long time since i last studied python and I forgot nearly everything – Ersin Burak Apr 06 '20 at 19:34
  • @ErsinBurak We use return so that later on we can save the output in a variable. for example, welcome() is returning name so later we can also save the return value in form of a variable. – Owais Qayum Apr 06 '20 at 19:45
  • @ErsinBurak, for your second question, when you are using return then you will be using the "print statement". If you are using "return" then you will use "print" as it saves space and is a good programming way. – Owais Qayum Apr 06 '20 at 19:49
  • But we're using the name here as a global variable, why are we returning global variables? If it was in the def it'd be understandable but I didn't know we should've returned it even though it was a global variable – Ersin Burak Apr 06 '20 at 19:59
  • Avoid globals especially when easy to do – B. Go Apr 06 '20 at 21:09
-1

You can declare the variable name as global variable.

Code -

def welcome():
    global name
    print("""Welcome to the low budget film recommender!
             Here you can tell me what kind of movies do you like or what movie did you watch
             and I'll suggest you a movie from my database according to that.""")
    name = input("But first I need to learn your name:>> ").capitalize()
    print(f"Nice to meet you {name}")
    return name


def second_function():
    welcome()
    print(name) #prints the value of name in this function which was defined in the welcome()

second_function()
  • Thank you so much that's exactly how I wanted to use it. I didn't know there was a thing called "global", thanks again – Ersin Burak Apr 06 '20 at 19:55