From tech troubles to travel tips, IDNLearn.com has answers to all your questions. Get thorough and trustworthy answers to your queries from our extensive network of knowledgeable professionals.

write a python code for a calculator

Sagot :

I am sure you are using an editor but if doesn’t work you can search online python compiler .

Answer :

We know that a calculater should be able to calculate ( addition , subs traction, multiplication, division …etc)

# first takng inputs
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# operations
print("Operation: +, -, *, /")
select = input("Select operations: ")

# check operations and display result
# add(+) two numbers
if select == "+":
print(num1, "+", num2, "=", num1+num2)

# subtract(-) two numbers
elif select == "-":
print(num1, "-", num2, "=", num1-num2)

# multiplies(*) two numbers
elif select == "*":
print(num1, "*", num2, "=", num1*num2)

# divides(/) two numbers
elif select == "/":
print(num1, "/", num2, "=", num1/num2)

else:
print("Invalid input")

Answer:

numb1 = int(input('Enter a number one: '))

Operation = input('Enter operation: ')

numb2 = int(input('Enter a number two: '))

def Calculator(numb1, Operation, numb2):

   if Operation == '+':

       print(numb1+numb2)

   elif Operation == '-':

       print(numb1-numb2)

   elif Operation == '×':

       print(numb1*numb2)

   elif Operation == '÷':

       print(numb1/numb2)

   elif Operation == '^':

       print(numb1**numb2)

   elif Operation == '//':

       print(numb1//numb2)

   elif Operation == '%':

       print(numb1%numb2)

   else:

       print('Un supported operation')

Calculator(numb1, Operation, numb2)

Explan ation:

Write a Python Calculator function that takes two numbers and the operation and returns the result.

Operation              Operator

Addition                    +

Subtraction               -

Multiplication            *

Division                     /

Power                       **

Remainder               %

Divide as an integer   //