python calculator

Solutions on MaxInterview for python calculator by the best coders in the world

showing results for - "python calculator"
Marta
14 Oct 2017
1from whiteCalculator import Calculator
2c = Calculator()
3print(c.run("1+8(5^2)"))
4# Output: 201
5print(c.run("9Ans"))
6# Output: 1809
Jill
06 Jun 2020
1num_one = int(input("Enter 1st number: "))
2
3op = input("Enter operator: ")
4
5num_two = int(input("Enter 2nd number: "))
6
7if op == "+":
8    print(num_one + num_two)
9elif op == "-":
10    print(num_one - num_two)
11elif op == "*" or op == "x":
12    print(num_one * num_two)
13elif op == "/":
14    print(num_one / num_two)
15
Kennedy
16 May 2017
1print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")
2num = int(input())
3if num == 1:
4    print("Enter Number 1 : ")
5    add1  = int(input())
6    print("Enter Number 2 : ")
7    add2 = int(input())
8    sum = add1 + add2
9    print("The Sum Is ", sum)
10elif num == 2:
11    print("Enter Number 1 : ")
12    sub1  = int(input())
13    print("Enter Number 2 : ")
14    sub2 = int(input())
15    difference = sub1 - sub2
16    print("The Difference Is ", difference)
17elif num == 3:
18    print("Enter Number 1 : ")
19    div1  = float(input())
20    print("Enter Number 2 : ")
21    div2 = float(input())
22    division = div1 / div2
23    print("The Division Is ", division)
24elif num == 4:
25    print("Enter Number 1 : ")
26    mul1 = int(input())
27    print("Enter Number 2 : ")
28    mul2 = int(input())
29    multiply = mul1 * mul2
30    print("The Difference Is ", multiply)
31else:
32    print("enter a valid Number")
Pablo
22 Oct 2017
1# define the function
2def calculate():
3    print('Please choose your operator adding(+) subtracting(-) multiplying(*) didviding(/) for power(**) for module(%)')
4
5    number_1 = int(input("Enter your first digit: "))
6    operation = input("Enter your operator: ")
7    number_2 = int(input("Enter your first digit: "))
8
9
10# adding
11    if operation == '+':
12        print('{} + {} = '.format(number_1, number_2))
13        print(number_1 + number_2)
14        print("You were adding.\n")
15        print("How do I know that I'm a smart progammer ;)\n")
16
17# subtracting
18    elif operation == '-':
19        print('{} - {} = '.format(number_1, number_2))
20        print(number_1 - number_2)
21        print("You were subtracting.\n")
22        print("How do I know that I'm a smart progammer ;)\n")
23
24# mulitplaying
25    elif operation == '*':
26        print('{} * {} = '.format(number_1, number_2))
27        print(number_1 * number_2)
28        print("You were mulitplaying.")
29        print("How do I know that I'm a smart progammer ;)\n")
30
31# dividing
32    elif operation == '/':
33        print('{} / {} = '.format(number_1, number_2))
34        print(number_1 / number_2)
35        print("You were dividing.\n")
36        print("How do I know that I'm a smart progammer ;)\n")
37
38# for power
39    elif operation == '**':
40        print('{} ** {} = '.format(number_1, number_2))
41        print(number_1 ** number_2)
42        print("You were using for power.\n")
43        print("How do I know that I'm a smart progammer ;)\n")
44
45# module
46    elif operation == '%':
47        print('{} % {} = '.format(number_1, number_2))
48        print(number_1 % number_2)
49        print("You were using module.\n")
50        print("How do I know that I'm a smart progammer ;)\n")
51
52    # if they get a error
53    else:
54        print("Your number you have typed is invalid, please restart your program!")
55    # add again() here as a function outside the calculate()
56    again()
57
58
59def again():
60    cal_again = input("Do you want to calculate again? Y = yes or N = no: ")
61
62    # Taking user input
63    if cal_again.upper() == 'Y':
64        calculate()
65    elif cal_again.upper() == 'N':
66        print('Leave kid ;-;')
67    else:
68        again()
69
70
71def welcome():
72    print("Welcome to my calculator made by Pepa pig lol made in python :D")
73
74
75# use calculate() for the function
76welcome()
77calculate()
Matisse
16 Jun 2017
1print('Calculator')
2input_1 = input('First Number? ')
3input_2 = input('Second Number? ')
4
5try:
6    print(f'{input_1} + {input_2} is {float(input_1) + float(input_2)}')
7    print(f'{input_1} - {input_2} is {float(input_1) - float(input_2)}') 
8    print(f'{input_1} X {input_2} is {float(input_1) * float(input_2)}')
9    print(f'{input_1} / {input_2} is {float(input_1) // float(input_2)}')
10except Exception as e:
11    print(f'ERROR: {e}')
12
Orson
19 Mar 2017
1import time
2def cal():
3    def add(x, y):
4        print(x + y)
5
6    def sub(x, y):
7        print(x - y)
8
9    def mul(x, y):
10        print(x * y)
11
12    def div(x, y):
13        print(x // y)
14
15    while True:
16        print('Adding?(+) Subtracting?(-) Multiplying?(*) Dividing?(/)')
17        answer = input('')
18        if answer == "+":
19            try:
20                x = float(input('First number! '))
21                y = float(input('Second Number! '))
22            except Exception as e:
23                print(f'Error: {e}')
24                time.sleep(5)
25                return
26            add(x, y)
27        elif answer == "-":
28            try:
29                x = float(input('First number! '))
30                y = float(input('Second Number! '))
31            except Exception as e:
32                print(f'Error: {e}')
33                time.sleep(5)
34                return
35            sub(x, y)
36        elif answer == "*":
37            try:
38                x = float(input('First number! '))
39                y = float(input('Second Number! '))
40            except Exception as e:
41                print(f'Error: {e}')
42                time.sleep(5)
43                return
44            mul(x, y)
45        elif answer == "/":
46            try:
47                x = float(input('First number! '))
48                y = float(input('Second Number! '))
49            except Exception as e:
50                print(f'Error: {e}')
51                time.sleep(5)
52                return
53            div(x, y)
54        else:
55            print('Error. Enter a valid input!')
56
57
58cal()
59
Brogan
24 Sep 2018
1 
2 #Simple faulty calculator
3
4# This function adds two numbers
5def add(x, y):
6    return x + y
7
8# This function subtracts two numbers
9def subtract(x, y):
10    return x - y
11
12# This function multiplies two numbers
13def multiply(x, y):
14    return x * y
15
16# This function divides two numbers
17def divide(x, y):
18    return x / y
19
20print("Select operation.")
21print("Add = +")
22print("Subtract = -")
23print("Multiply = *")
24print("Divide = /")
25
26while True:
27    # Take input from the user
28    choice = input("Enter choice(+ or - or * or /): ")
29
30    # Check if choice is one of the four options
31    if choice in ('+', '-', '*', '/'):
32        num1 = float(input("Enter first number: "))
33        num2 = float(input("Enter second number: "))
34
35        if choice == '+':
36            print(num1, "+", num2, "=", add(num1, num2))
37
38        elif choice == '-':
39            print(num1, "-", num2, "=", subtract(num1, num2))
40
41        elif choice == '*':
42            print(num1, "*", num2, "=", multiply(num1, num2))
43
44        elif choice == '/':
45            print(num1, "/", num2, "=", divide(num1, num2))
46        break
47    else:
48        print("Invalid Input")
Amelia
26 Mar 2019
1#Store number variables for the two numbers
2
3num1 = input('Enter first number: ')
4num2 = input('Enter second number: ')
5
6#the sum of the two numbers variable
7sum = float(num1) + float(num2)
8sum2 = float(num1) - float(num2)
9sum3 = float(num1) * float(num2)
10sum4 = float(num1) / float(num2)
11
12#what operator to use
13choice = input('Enter an operator, + = addition, - = subtraction, * = multiplication and / = division: ')
14#different sums based on the operators
15if choice == '+':
16  print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
17
18  if choice == '-':
19    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum2))
20
21if choice == '*':
22    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum3))
23
24if choice == '/':
25    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum4))
Montserrat
26 Feb 2016
1import math 
2def SquareRoot (x):
3   return math.sqrt(x)
4  
5
6def lcm(x, y):  
7   
8    if x > y:  
9        greater = x  
10    else:  
11        greater = y  
12    while(True):  
13        if((greater % x == 0) and (greater % y == 0)):  
14            lcm = greater  
15            break  
16        greater += 1  
17    return lcm  
18
19def hcf(a,b):
20    H=a if a<b else b
21    while H>=1:
22        if a%H==0 and b%H==0:
23            return H
24        H-=1
25     
26
27def add(x, y):
28    return x + y
29
30
31def subtract(x, y):
32    return x - y
33
34
35def multiply(x, y):
36    return x * y
37
38
39def divide(x, y):
40    return x / y
41
42
43print("Select operation.")
44print("1.Add")
45print("2.Subtract")
46print("3.Multiply")
47print("4.Divide")
48print("5.lcm")
49print("6.SquareRoot")
50print("7.hcf")
51
52
53while True:
54    
55    choice = input("Enter choice(1/2/3/4/5/6/7): ")
56
57    
58    if choice in ('1', '2', '3', '4','5','7'):
59        num1 = float(input("Enter first number: "))
60        num2 = float(input("Enter second number: "))
61
62        if choice == '1':
63            print(num1, "+", num2, "=", add(num1, num2))
64
65        elif choice == '2':
66            print(num1, "-", num2, "=", subtract(num1, num2))
67
68        elif choice == '3':
69            print(num1, "*", num2, "=", multiply(num1, num2))
70
71        elif choice == '4':
72            print(num1, "/", num2, "=", divide(num1, num2))
73        
74        elif choice == '5':
75          print("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2)) 
76
77        elif choice == '7':
78          print("The H.C.F of", num1,"and", num2,"is", hcf(num1, num2)) 
79
80        
81        next_calculation = input("Do you want to do another calculation (yes/no): " )
82        if next_calculation == "no":
83          break
84
85    elif choice in ('6'):
86        num0 = float(input("Enter a number: "))
87        
88        if choice == '6':
89            print("The SquareRoot of ",num0,"is",SquareRoot(num0))
90
91    else:
92        print("Invalid Input")
Niclas
07 Jan 2021
1# simple calculator
2try:
3  # prompt user
4    num1 = float(input("Please Enter The first number: "))
5    operator = input("Please choose the operator (+) for addition, (-) for subtraction, (*) for multiplication"
6                     "(/) for division , (%) for remainder: ")
7    num2 = float(input("Please Enter The second number: "))
8    
9    if operator == '+':
10        result = num1 + num2
11        print("The result is: ", result)
12    elif operator == '-':
13        result = num1 - num2
14        print("The result is: ", result)
15    elif operator == '*':
16        result = num1 * num2
17        print("The result is: ", result)
18    elif operator == '/':
19        try:
20            if True:
21                result = num1 / num2
22                print(result)
23        except ZeroDivisionError as err:
24            print(err, " oops! zero division occur ")
25    elif operator == '%':
26        if operator == '%':
27            result = num1 % num2
28            print("The result is: ", result)
29    else:
30        raise TypeError
31
32
33except ValueError:
34    print("wrong value, suggest integer or decimal")
35finally:
36    print("All Done")
37