Aravinda KS
Aravinda KS

Reputation: 195

Is it possible to pass the function inside the format function? Code exiting with error code zero

def eight():
    return 8
def nine():
    return 9

def plus():
    print({ } + { }.format(eight(),nine()))

Is this a correct syntax? Can we pass a function inside format function, if i run this code it is not performing anything instead it is coming out with exit code 0. My expected output is

17   //which is 8+9

the output which i am getting is:

Process finished with exit code 0

The plus() function should perform addition of two numbers, i am passing the numbers in the form of functions, whenever I call a function like eight() it should return the number eight. Basically I am performing calculation using functions. Can anyone help me with this? Whether the syntax I used for format is correct or not? And is this the correct way. Thank you

Upvotes: 0

Views: 71

Answers (2)

mhhabib
mhhabib

Reputation: 3121

Python supports the format() function for using only the built-in string class provides the ability to do complex variable substitutions and value formatting via the format().

To see more details please go through the link

You can use the below format to get the result.

def eight():
   return 8
def nine():
   return 9
def plus():
   print("{}".format(eight()+nine()))

You can also use to see the calculative format

def eight():
   return 8
def nine():
   return 9
def plus():
   print("{0} + {1} = {2}".format(eight(),nine(), eight()+nine()))

Upvotes: 0

Aaj Kaal
Aaj Kaal

Reputation: 1274

Code:

def f1():
    print('f1')
    return 'f1-done'
    
print('f1 returned %s' % f1())

    

Output:

f1
f1 returned f1-done

Edit:

Code:

def eight():
    return 8
def nine():
    return 9

def plus():
    print('%d' % (eight() + nine()))   # 17

plus()



    

Upvotes: 1

Related Questions