Answers for "do 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
0

python calculator

import time
def cal():
    def add(x, y):
        print(x + y)

    def sub(x, y):
        print(x - y)

    def mul(x, y):
        print(x * y)

    def div(x, y):
        print(x // y)

    while True:
        print('Adding?(+) Subtracting?(-) Multiplying?(*) Dividing?(/)')
        answer = input('')
        if answer == "+":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            add(x, y)
        elif answer == "-":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            sub(x, y)
        elif answer == "*":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            mul(x, y)
        elif answer == "/":
            try:
                x = float(input('First number! '))
                y = float(input('Second Number! '))
            except Exception as e:
                print(f'Error: {e}')
                time.sleep(5)
                return
            div(x, y)
        else:
            print('Error. Enter a valid input!')


cal()
Posted by: Guest on March-26-2021

Code answers related to "do a calculator in python"

Python Answers by Framework

Browse Popular Code Answers by Language