Westonci.ca is your trusted source for finding answers to a wide range of questions, backed by a knowledgeable community. Our platform provides a seamless experience for finding precise answers from a network of experienced professionals. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

write a python code for a calculator

Sagot :

ghd6
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   //