Can someone teach me how to make this program work correctly?

+1 vote
asked Mar 18 by anonymous
When i try to calculate (square) of number, (Enter first number) is executed first.

-----------------------------------------------------------------------------------------------------------------

# Calculator
def square():
  number=int(input('Enter the number:'))
  squ=(number*number)
  print(squ)
  
print('''1. Addition
         2.Subtraction
         3.Multiplication
         4.Division
         5.Square''')
option=input('Enter the option:')

num1=int(input('Enter the first number:'))
num2=int(input('Enter the second number:'))

if option == '1':
  print('{} + {} = '.format(num1,num2))
  print (num1+num2)
elif option == '2':
  print('{} - {} = '.format(num1,num2))
  print (num1_num2)
elif option == '3':
  print('{} * {} = '.format(num1,num2))
  print (num1*num2)
elif option == '4':
  print('{} / {} = '.format(num1,num2))
  print (num1/num2)
elif option =='5':
  square()
else:
  exit()
  
square()

2 Answers

+1 vote
answered Mar 25 by KV's Fun Time (460 points)
 
Best answer

Hi... just went through your question. The code written below will solve your issue:-

def ask_operation():
    option = input('''
Enter the Serial Number of the
operation you want to perform''')

    if option == "1":
        addition()
    elif option == "2":
        subtraction()
    elif option == "3":
        multiplication()
    elif option == "4":
        division()
    elif option == "5":
        square()
    else:
        print("The operation you entered is not listed above")
        ask_operation()

def addition():
    num1 = int(input("Enter 1st number: "))
    num2 = int(input("Enter 2nd number: "))
    ans = num1 + num2
    print("{} is the sum".format(ans))
    
def subtraction():
    num1 = int(input("Enter 1st number: "))
    num2 = int(input("Enter 2nd number: "))
    ans = num1 - num2
    print("{} is the diffrence".format(ans))

def multiplication():
    num1 = int(input("Enter 1st number: "))
    num2 = int(input("Enter 2nd number: "))
    ans = num1 * num2
    print("{} is the product".format(ans))
    
def division():
    num1 = int(input("Enter 1st number: "))
    num2 = int(input("Enter 2nd number: "))
    ans = num1 / num2
    print("{} is the quotient".format(ans))
    
def square():
    num = int(input("Enter the number you want to sqaure: "))
    ans = num ** 2
    print("{} is the square of {}".format(ans, num))
        
    
print('''
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Square
''')
ask_operation()


I hope you will be satisfied

0 votes
answered Mar 23 by Debjani Baidya (220 points)
print ("Press 1 - for Addition")
print ("Press 2 - for Subtraction")
print ("Press 3 - for Multiplication")
print ("Press 4 - for Division")
print ("Press 5 - for Finding square")

num1 = input ("Enter a Choice: ")
if num1 == '1' :
     numb1 = input ("Enter a number to add: ")
     numb2 = input ("Enter another number to add: ")
     sum = float(numb1)+float(numb2)
     print ('The sum of {0} and {1} is {2}'. format(numb1, numb2, sum))
     
     
elif num1 == '2' :
     numb1 = input ("Enter a number to subtract: ")
     numb2 = input ("Enter another number to subtract: ")
     difference = float(numb1)-float(numb2)
     print ('The difference of {0} and {1} is {2}'. format(numb1, numb2, difference))

elif num1 == '3' :
     numb1 = input ("Enter a number to multiply: ")
     numb2 = input ("Enter another number to multiply: ")
     mul = float(numb1)*float(numb2)
     print ('The product of {0} and {1} is {2}'. format(numb1, numb2,mul))

elif num1 == '4' :
     numb1 = input ("Enter a number to divide: ")
     numb2 = input ("Enter another number to divide: ")
     div = float(numb1)/float(numb2)
     print ('The quotient of {0} and {1} is {2}'. format(numb1, numb2, div))

elif num1 == '5' :
     numb1 = input ("Enter a number : ")
     sqr = float(numb1)*float(numb1)
     print ('{0} square is {2}'. format(numb1, sqr))
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...