0
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

2
  • Do you need print(eight() + nine())? Commented Nov 17, 2020 at 2:42
  • Yes @fas But not directly using print statement, but through add function created. Commented Nov 17, 2020 at 4:52

2 Answers 2

1

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()



    
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't get the answer, the function will call like this --> seven(plus(two())), this should perform the desired operation. I think we need to use lambda and do, which I am not getting.
0

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()))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.