185,214 views
10 votes
10 votes
Write a python code for a calculator

User Tatyana
by
3.1k points

2 Answers

18 votes
18 votes

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

User Borice
by
2.6k points
24 votes
24 votes
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")
User BrianCooksey
by
2.4k points