Is there any mistake in this Code ? if it is let me know?

a = int(input("Enter Your First Number=")) b = int(input("\nEnter Your Second Number="))  def cal():     sum = a + b     sub = a - b     mul = a * b     div = a % b  cal()     print("\nAddtion of Two Number is =" , sum) print("\nSubtraction of Two Number is =" , sub ) print("\nMultiplication of Two Number is =" , mul  ) print("\nDivision of Two Number is =" , div  ) 

I can’t find the exact output I’m a beginner So guide me please,

Add Comment
3 Answer(s)

You need to make the function return the values:

a = int(input("Enter Your First Number=")) b = int(input("\nEnter Your Second Number="))  def cal():     sum = a + b     sub = a - b     mul = a * b     div = a % b     return sum, sub, mul, div  sum, sub, mul, div = cal()     print("\nAddtion of Two Number is =" , sum) print("\nSubtraction of Two Number is =" , sub ) print("\nMultiplication of Two Number is =" , mul  ) print("\nDivision of Two Number is =" , div  ) 
Add Comment

You have to return the values in order to use them outside the function

a = int(input("Enter Your First Number=")) b = int(input("\nEnter Your Second Number="))  def cal():     return a + b, a - b, a * b, a % b  sum,sub,mul,div = cal()     print("\nAddtion of Two Number is =" , sum) print("\nSubtraction of Two Number is =" , sub ) print("\nMultiplication of Two Number is =" , mul  ) print("\nDivision of Two Number is =" , div  ) 
Add Comment

You cant acces function variables outside of function. PLace print statements inside a function or return values from the function as mentioned below.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.