basic python calculator code example

Example 1: 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))

Example 2: python calculator

print('Calculator')
input_1 = input('First Number? ')
input_2 = input('Second Number? ')

try:
    print(f'{input_1} + {input_2} is {float(input_1) + float(input_2)}')
    print(f'{input_1} - {input_2} is {float(input_1) - float(input_2)}') 
    print(f'{input_1} X {input_2} is {float(input_1) * float(input_2)}')
    print(f'{input_1} / {input_2} is {float(input_1) // float(input_2)}')
except Exception as e:
    print(f'ERROR: {e}')

Example 3: python calculator

# define the function
def calculate():
    print('Please choose your operator adding(+) subtracting(-) multiplying(*) didviding(/) for power(**) for module(%)')

    number_1 = int(input("Enter your first digit: "))
    operation = input("Enter your operator: ")
    number_2 = int(input("Enter your first digit: "))


# adding
    if operation == '+':
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
        print("You were adding.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# subtracting
    elif operation == '-':
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
        print("You were subtracting.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# mulitplaying
    elif operation == '*':
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
        print("You were mulitplaying.")
        print("How do I know that I'm a smart progammer ;)\n")

# dividing
    elif operation == '/':
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
        print("You were dividing.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# for power
    elif operation == '**':
        print('{} ** {} = '.format(number_1, number_2))
        print(number_1 ** number_2)
        print("You were using for power.\n")
        print("How do I know that I'm a smart progammer ;)\n")

# module
    elif operation == '%':
        print('{} % {} = '.format(number_1, number_2))
        print(number_1 % number_2)
        print("You were using module.\n")
        print("How do I know that I'm a smart progammer ;)\n")

    # if they get a error
    else:
        print("Your number you have typed is invalid, please restart your program!")
    # add again() here as a function outside the calculate()
    again()


def again():
    cal_again = input("Do you want to calculate again? Y = yes or N = no: ")

    # Taking user input
    if cal_again.upper() == 'Y':
        calculate()
    elif cal_again.upper() == 'N':
        print('Leave kid ;-;')
    else:
        again()


def welcome():
    print("Welcome to my calculator made by Pepa pig lol made in python :D")


# use calculate() for the function
welcome()
calculate()