Brenan Class
Brenan Class

Reputation: 1

I don’t understand why this won’t print, it says that title is not defined on line 5 even though I defined it in the parenthesis

I defined the problem in the title oops

I have tried so many things I can’t even write them all.

def document(title="cool", genre="fiction"):
  print(title+genre)

document(title = "once upon a time ")
document(“awesome”)
document(title+genre)

I expect it to print, once upon a time awesome, cool fiction.

Upvotes: 0

Views: 72

Answers (3)

Milad Barazandeh
Milad Barazandeh

Reputation: 331

You defined a function which takes two arguments named title and genre. These two arguments are only accessible within your function as local variable. Since these variables aren't declared outside the function, they can not be accessed.

def document(title="cool", genre="fiction"):
  print(title+genre)

#declaration of variables
title="foo"
genre="bar"
document(title, genre)

Upvotes: 1

smci
smci

Reputation: 33938

You're confusing defining a variable (e.g. title = "once upon a time") with specifying a function argument document(title="whatever"). The latter only specifies the argument passed into document() function, it doesn't define a local variable called title.

And by "inside the parenthesis" you mean "in my function call to document()"

One solution is to do the following:

title = "once upon a time "  # <-- actually define a local variable
genre = "Mongolian puppetry"

document(title)  # now you can use that variable in your function call
document(“awesome”)
document(title+genre)  # ...and reuse that variable again

Upvotes: 0

Karl Knechtel
Karl Knechtel

Reputation: 61515

def document(title="cool", genre="fiction"):

This means that the function has two arguments, named title and genre. Each of these has a default value provided, that will be filled in if the caller does not provide them. This is not "defining title" in the way that you seem to be thinking of it. Each function has its own entirely separate set of names for things, as well as the global set of names for things.

    print(title+genre)

This means that whatever values were provided, whether they were from the caller or the default values, will be concatenated and printed.

document(title = "once upon a time ")

This says to call the function and use "once upon a time " as the value for title. The value for genre is not provided, so the default value of "fiction" is used. Thus,once upon a time fiction` is printed.

document("awesome")

This says to call the function and use "awesome" as the value for the first parameter. That parameter is the title parameter, so "awesome" is used as the value for title. As before, the value for genre is still "fiction", so awesomefiction is printed.

Note that when the function runs, title is the name that is being used by the function for the string "awesome", even though you didn't say anything about title when you called the function.

document(title+genre)

This says to use whatever are the values of title and genre in the calling context, as the value for the first parameter. But there are no such defined names outside the function. The function's parameters are completely separate and have no meaning whatsoever here. You get a NameError because the names in question are not defined.

Upvotes: 0

Related Questions