Answers for "write a python program for calculator"

7

simple python calculator

#Store number variables for the two numbers

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

#the sum of the two numbers variable
sum = float(num1) + float(num2)
sum2 = float(num1) - float(num2)
sum3 = float(num1) * float(num2)
sum4 = float(num1) / float(num2)

#what operator to use
choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
#different sums based on the operators
if choice == '+':
  print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

  if choice == '-':
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum2))

if choice == '*':
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum3))

if choice == '/':
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum4))
Posted by: Guest on October-29-2020
0

how to make a calculator in python

#You can change Text at Inputs

num1 = float(input("Enter Firt Number:"))
num2 = float(input("Enter Second Number"))

result= 0
op = str(input("Enter an op(operator)"))
#Gets Input Type--------
try:
  #U can make this in while loop ;-)
  if op=="+":
    result = num1 + num2
  else if op=="-":
    result = num1 - num2
  else if op=="*":
    result = num1 * num2
  else if op=="/":
    result = num1 / num2
  else:
      print("Invalid Input")
  print("The result is " + result)
except ZeroDivisionError:
  print("U cant do that")
Posted by: Guest on May-25-2021

Code answers related to "write a python program for calculator"

Python Answers by Framework

Browse Popular Code Answers by Language