1

I seek advice on a matter relating to this function.

I have tried various editing and indentation on the code, but it is showing the same result.

It shows NameError: name 'sentence' is not defined although I have defined it in the function.

The code is:

def about (name, age, likes):
  sentence = "Meet {}, he is {} years old and likes {}".format(name,age,likes)
  return sentence

about ("Jack", 23, "programming")
print (sentence)
martineau
  • 119,623
  • 25
  • 170
  • 301
Mafruh Faruqi
  • 15
  • 1
  • 4

2 Answers2

3

You should call function and assign it to an variable:

def about(name, age, likes):
    sentence = "Meet {}, he is {} years old and likes {}".format(name,age,likes)
    return sentence

Then

val = about("Jack", 23, "programming")
print(val)

you can also use sentence instead of val but this will not be the same sentence in function scope.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
1

try this now....

def about (name, age, likes):
    sentence = "Meet {}, he is {} years old and likes {}".format(name,age,likes)
    return sentence

print(about('rohit',23,'programming'))

sentense scope is bounded to about function... and trying to print it out of the function scope may not work.

Karthikeyan K
  • 229
  • 2
  • 7