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
  • 112,593
  • 23
  • 157
  • 280
Kawsar Mafruh
  • 15
  • 1
  • 3

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
  • 9,989
  • 4
  • 33
  • 55
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
  • 209
  • 2
  • 7