Answers for "how to build a calculator in python"

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
6

python calculator

num_one = int(input("Enter 1st number: "))

op = input("Enter operator: ")

num_two = int(input("Enter 2nd number: "))

if op == "+":
    print(num_one + num_two)
elif op == "-":
    print(num_one - num_two)
elif op == "*" or op == "x":
    print(num_one * num_two)
elif op == "/":
    print(num_one / num_two)
Posted by: Guest on August-01-2020
2

how to make a calculator using idle

def calculate():
    operation = input('''
Please type in the math operation you would like to complete:
+ for addition
- for subtraction
* for multiplication
/ for division
''')

    number_1 = int(input('Please enter the first number: '))
    number_2 = int(input('Please enter the second number: '))

    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)

    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)

    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)

    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)

    else:
        print('You have not typed a valid operator, please run the program again.')

    # Add again() function to calculate() function
    again()

def again():
    calc_again = input('''
Do you want to calculate again?
Please type Y for YES or N for NO.
''')

    if calc_again.upper() == 'Y':
        calculate()
    elif calc_again.upper() == 'N':
        print('See you later.')
    else:
        again()

calculate()
Posted by: Guest on April-06-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
0

simple calculator in python

#Simple Calculator Program in Python by Rupesh Singh

def sum1(first_input, second_input):    # Sum function "+"
    result1 = first_input + second_input
    print("Your Answer is:-", result1)
    return

def minus(first_input, second_input):    # Minus function "-"
    result2 = first_input - second_input
    print("Your Answer is:-", result2)
    return

def multi(first_input, second_input):    # Multiplication function "*"
    result3 = first_input * second_input
    print("Your Answer is:-", result3)
    return

def div(first_input, second_input):    # Division function "/"
    result4 = first_input / second_input
    result4 = float(round(result4))
    print("Your Answer is:-", result4)
    return

# User Input
first_input = int(input("Enter your 1st Input:- "))
command = input("What you want to do (+, -, *, /):- ")
second_input = int(input("enter your 2nd Input:- "))

if command == "+":
    sum1(first_input, second_input)
elif command == "-":
    minus(first_input, second_input)
elif command == "*":
    multi(first_input, second_input)
elif command == "/":
    div(first_input, second_input)
else:
    print("Command Error!")
Posted by: Guest on April-03-2021
0

calculator with python

#Simple python Calculator
#Enter the first variable
#then the operator
#and finally the second variable, then the answer is printed.
A = int(input("Enter your first number: "))
operator = input("Which operation would you like to perform? (+, -, /, *, //, **): ");
B = int(input("Enter your second number: "))
#Addition
if operator == "+":
    ans = A + B
    print(str(ans))
#Subraction
elif operator == "-":
    ans = A - B
    print(str(ans))
#division
elif operator == "/":
    ans = A / B
    print(str(ans))
#Multiplication
elif operator == "*":
    ans = A * B
    print(str(ans))
#Integer division
elif operator == "//":
    ans = A // B
    print(str(ans))
#Exponent
elif operator == "**":
    ans = A ** B
    print(str(ans))
else:
    print("wrong operator!")
Posted by: Guest on April-21-2021

Code answers related to "how to build a calculator in python"

Python Answers by Framework

Browse Popular Code Answers by Language