how to make a calculator in python

Solutions on MaxInterview for how to make a calculator in python by the best coders in the world

showing results for - "how to make a calculator in python"
Arianna
21 Nov 2020
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))
26 
Lizbeth
07 Jan 2018
1from tkinter import *
2import random
3import time
4
5def btnClick(numbers):
6	global operator
7	operator = operator + str(numbers)
8	text_Input.set(operator)
9
10def bcd():
11	global operator
12	operator = ""
13	text_Input.set("")
14
15def bei():
16	global operator
17	sumup = str(eval(operator))
18	text_Input.set(sumup)
19	operator = sumup
20
21
22root = Tk()
23root.title("Calculator")
24root.resizable(False, False)
25
26operator = ""
27text_Input = StringVar()
28
29#AnsShow
30textDisplay = Entry(root, font = ('arial', 20, 'bold'), textvariable = text_Input, bd = 30, insertwidth = 4, bg = "red", justify = 'right')
31textDisplay.grid(columnspan = 4)
32
33#button
34btn7 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "7", command = lambda:btnClick(7)).grid(row = 1, column = 0)
35
36btn8 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "8", command = lambda:btnClick(8)).grid(row = 1, column = 1)
37
38btn9 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "9", command = lambda:btnClick(9)).grid(row = 1, column = 2)
39
40Add = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "+", command = lambda:btnClick("+")).grid(row = 1, column = 3)
41
42#============================================================================================================================#
43btn4 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "4", command = lambda:btnClick(4)).grid(row = 2, column = 0)
44
45btn5 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "5", command = lambda:btnClick(5)).grid(row = 2, column = 1)
46
47btn6 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "6", command = lambda:btnClick(6)).grid(row = 2, column = 2)
48
49Sub = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "-", command = lambda:btnClick("-")).grid(row = 2, column = 3)
50
51#===============================================================================================================================#
52btn1 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "1", command = lambda:btnClick(1)).grid(row = 3, column = 0)
53
54btn2 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "2", command = lambda:btnClick(2)).grid(row = 3, column = 1)
55
56btn3 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "3", command = lambda:btnClick(3)).grid(row = 3, column = 2)
57
58Multiply = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "*", command = lambda:btnClick("*")).grid(row = 3, column = 3)
59
60#==================================================================================================================================#
61btn0 = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "0", command = lambda:btnClick(0)).grid(row = 4, column = 0)
62
63equal = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "=", command = bei).grid(row = 4, column = 1)
64
65divide = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "/", command = lambda:btnClick("/")).grid(row = 4, column = 2)
66
67clear = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "c", command = bcd).grid(row = 4, column = 3)
68
69root.mainloop()
Mattia
24 Jan 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")
Gustave
17 Oct 2017
1#Calculator
2
3import time
4
5def add():
6  n1 = input('Enter a number: ')
7  n2 = input('Enter another number: ')
8  time.sleep(1)
9  print(int(n1)+int(n2))
10  
11def multiply():
12  n1 = input('Enter a number: ')
13  n2 = input('Enter another number: ')
14  time.sleep(1)
15  print(int(n1)*int(n2))
16    
17def divide():
18  n1 = input('Enter a number: ')
19  n2 = input('Enter another number: ')
20  time.sleep(1)
21  print(int(n1)int(n2))
22  
23def subtract():
24  n1 = input('Enter a number: ')
25  n2 = input('Enter another number: ')
26  time.sleep(1)
27  print(int(n1)-int(n2))
28  
29run = True
30while run:
31  ans = input('Select an opreation: ')
32  if(ans=='Addition'):
33    add()
34    
35  if(ans=='Divide'):
36    divide()
37    
38  if(ans=='Multiply'):
39    multiply()
40    
41  if(ans=='Subtract'):
42    subtract()
43  
44  
45  
Alberto
20 Mar 2016
1# This will be one of the most advanced results you will find.
2
3# We will be using classes and simple operators like +,-,*,/
4
5class Calculator:
6  def addition(a,b):
7    return a + b
8
9  def subtraction(a,b):
10    if a<b:
11      return b - a
12    else:
13      return a - b
14
15  def multiplication(a,b):
16    return a * b
17
18  def division(a,b):	
19    if a<b:
20      return b / a
21    else:
22      return a / b
23
24# You can do this in terminal.
25<C:/Users/username>python
26>>> from main import Calculator
27>>> result = Calculator.[addition|subtraction|multiplication|division](anyNumber, anyNumber)
28>>> print(result)
Luisa
25 Apr 2018
1#You can change Text at Inputs
2
3num1 = float(input("Enter Firt Number:"))
4num2 = float(input("Enter Second Number"))
5
6result= 0
7op = str(input("Enter an op(operator)"))
8#Gets Input Type--------
9try:
10  #U can make this in while loop ;-)
11  if op=="+":
12    result = num1 + num2
13  else if op=="-":
14    result = num1 - num2
15  else if op=="*":
16    result = num1 * num2
17  else if op=="/":
18    result = num1 / num2
19  else:
20      print("Invalid Input")
21  print("The result is " + result)
22except ZeroDivisionError:
23  print("U cant do that")
24
queries leading to this page
build a calculator using pythonpython simple calculator whith only integerhow to make a calculator in pythonbasic calculator using pythoncalculater app in pythoncode for making calculator in pythonpython code to make a calculatorpython calculas programhow to create a calculator program in pythonsimple logic for calculatorhow to code calculator in python without functionpython how to make a calculatorcalculator on pythonpython input 28 29 to calculatorcalculatror pythondo a calculator in pythonsimple calculator in python using swithcerimplement a calculator in pythnhow to make calculator by using pythonpython basic calculatorpython calculatorsimple basic calculator with pythoncalculator that runs pythonhow to code a python calculatorhow to add a calculation in pythoncreating a calculator in pythonpython program to make a calculatorcoding a simple calculator in pythonpython calculator display a menu with operation options inputhow to make calculator in pythonbuilding calculator pythonpython module calculator programsimple calculator pythonpython program to make simple calculatorbuilding a calculator in pythoncalculator code in python for python calculatiorcalculator using pythonbest python calculatorimplement calculatorhow to make a simple calcualator in pythonuse built in calculator pythoncan you code a calculator in pythoncreate a calculator where user have a menu to perform addition 2f subtraction 2f multiplication 2f division 2f display tables using loops in python python program to create a calculatorcalculator program in pythondoes python have calculatorcreate a calculator in pythonhow to do calculator in pythonsimple calculator code in pythonhow to run python calculator codecalculator app pythontaking an operation as input how to calculate pythonpython calculator programcalculator code in pythonhomework 5 3a multiplication calculator pythoncalculator python code glebber code for calculatorhow to make a python calculatormaking a simple calculator in pythonpython script calculatormake console calculator in pythoncalculator in python functionstrcutured package for calculator using pythonhow to create a simple calculator in pythonhow to make a calculator inpycalculator making in pythonhow to use python as a calculator the programe should performe the operation selectedsimple calculator with pythonhow to make a addition calculator in pythondo a calculator in pythobasic py calculatorpython make a calculatorcalculator program in python using functionssimple calculator using pythoncalculator price in python using functionshow to make a simple python calculatorpython projects using calculationspython input calculatorsimple calculator project in pythoncalculator syntax for pythonpython calculator with classescomputation code pythonhow to create a web based calculator with pythonfunction to calculate python calculator in pythonmake calculator pythobcalculator python codecreate calculator in pythonhow to make a coucalator pythonhow to make a calculator in python source codecode for calculator in pythonwrite a calculator code in pythonpython calculator examplepython program to make calculatorpython calcmaking calculator in pythonmaking a calculator in pythonhow to use python on your calculatorcalculator algorithm pythoncode to make a calculator in pythonprograming a calculator to pythonhow to make calculator in pythone xplainedmaking a text based calculator in pythonoperations examples for calculator projectcalculator basic in pythongenerate python function given input and output data online calculatorpython as a calculator codecalculate x pythonmake a caleculatoer in pythonpython calcualtorpython simple calculator program2 09python program to make a simple calculatorhow to code a calculator in pythonpython program to build calculatorvlsm calculator pythonhow to write calculator program in pythonwhat do you need to create a python calculatorhow to give input into calc exe using pythonhow to make python calculatorcalculate function do in pythonpython list calculatormake calculator pythonpython calculator file linecalculator python 5ccan you make a calculator using pythonpython calorie calculatorbuild calculator with pythonpython program to simple calculatorhow to make a calculator using pythoncalculator using functions in pythonbasic calculator which can perform 2 or 3 number calculations pythoncalculator pythonsimple calculator using function in pythoncreate a calculator from modules in pythonbasic calculator pythoncode in python for calculatorhow to code calculator in pythonpython calculatorsimple calculator docs pythonhow to build a calculator in pythonmake a simple calculator in pythonhow to add a calculation function in pythonpython def calculatorpython module calculatorpython calculator code copy and pastepython calculataorwrite a program to create a simple calculator in python python arithmic calcilatorhow to make a simple calculator using pythonbasic calculator with pythonwrite a python program to design a simple calculator 28using functions 29 coding on a calculaotr in pythonpython estimator programcode calculator pythoncreate bsaic calculator pythonhow to create a calcualtor in python3basical calculator pythoncalculator codes in pythonpython calculator functionhow to make a calculator pythonsimple calculator python scriptcode calculatrice pythonhow to make calculator pythonhow to make a calculator in python sim0oewrite a python program to demonstrate basic calculator functionality using the concept of functions in python simple calculator command in pythonmaking a calculator in python using functionscool calculators we can create with pythonpython calculator example with chekccreating calculator in pythonpy calculatorpython simple calculator expressionhow to write calculations in pythonhow to make a basic calculator in pythonpython as a calculatorbesic calculator in phytoncreate a function calculator in pythonhow to code a simple calculator in pythonpython calculator codeswrite a python program for calculatormake a calculator in pythononline python calculatorhow to make python calculator which calculates two or 3 valuescalculator program pythoncreating a simple calculator in python python interpreter as calculator codehow to make simple calculator in pythonpython program calculatorcalculate lcpd pythoncalculator module pythoncalculator pythonbuilding calculator in pythonuse in python calculationwriting a calculator in pythonhow to make a simple calculator using functions in pythonhow to make basic calculator in pythonpython script for calculatorptython code of calculatorcalculator coding pythonhow to create a calulator in pythoncalculator questions program in pythonbuild a calculator in pythonsimple calculator python codeimplement basic calculator pythonpython as calculatorcalculator in python projectcalculators that use pythoncalculator in python codepyhton calculatorhow to calculate using pythonpython build calculator stringdef function python calculationcalculator project in pythonhow to make calculator with pythoncode for a calculator in pythoncalculate in python and display in ccalcualator with pythonhow to make an ode calculator pythonrun calculator pythoncalculator in pyhtoncalculator pyhton projectpython code for a working calculatorpython calculator program on pythoncode python calculatorimport calculator in pythonpython calculator sample codepython simple calculator with functionscreate calculator pythonhow make a calculator in pythoncalculator python programmake calculator with pythoncalculator in python for beinerscreate a calculator using pythoncalculator in pythoncalculator python projectcreate a calculator function pythonpython create function that calculatespyton calculator codecalculator on pyhotnpython code for a calculatorpython code for calculatorpython program make calculatorpython program to basic calculatorconstruct a python program to make a simple calculator complication pyhton calculatorarithmetic calculator pythoncalculator script pythoncalculatord you can do with pythoncalculator python source codeinput calculator pythoncreate your own calculator function pythonbuild simple calculator using pythoncalcaulator in pythonhow to make a calculator in puthoncalc function in pythonbasic calculator in python using functionspython code for calculator downloading filepython make calculatorpython simple calculatorcalculus calculator in pythonsimple python program for calculatoronline calculator using pythonpython program to make a simple calculator 28using functions 29calculator code pythonhow to create calculator app in pythoncalculator in pythpython calculator scriptpython code to creae a calculatorpythin calc packagepython calculator anshow to write a calculator program in pythoncode for calculator app in pythonhow to make a calculator in python for beginnersprogram calculatorcalculator excersise in pythoncalculator python 3python program that creates estimatorsimple calculator in python calculator phythoncalculator python scriptpython calculator code downloadpython calculator modulepython code calculatorsimple calculator in python using functionspython calaculatorpythion calculatorbasic python calculatorhow to make your own calculator softwarehow to create calculator in pythoncalculator that use python to programpython calculator codeingsimple calculator program in pythonpython program for simple calculatormake a basic calculator in python basic calculator in pythongenerating a calculator with pythoncalculatore pythonsimple caculation pythonpython caculatorsimple python calculatorcode for python calculatorsimple calculator operations pythonhow to build a calculator in pythoncalculator made with pythonhow to create a calculator in python3python program to design a simple calculator 28using functions 29 how to create python calculatorcalc in pythonhow to make calculator pytoncalculator in python3how to make a calcualtor pythoncalculator coding in pythonhow to make 22function calculator 22 in pythonpython code for calcculatorhow to open and operate calculator with pythonpython calculator codecalculatrice with pythonpython console calculatorhow to make a calculator python 2calculator module in pythoncalculator with pythonpython calculator basic codecalculator program in python using functionmake a simple calculator using python mathematical operations best python calculatorshow to create a calculator in pythoncalculator in pythonbhow to make a silmple calculaytor in pythonmaking a python calculatorscript for calculator in pythonhow to make an calculator in pythonwith what module can i create calculator in pythoncalculator code python 3how to make a simple calculator in pythonhow to do a calculator in pythonhow to build a calculator in pytonpackages needed to build calculator pythonwrite a calculator in pythonpython calculaor filecalculat pythonhow to make a boolean function in pythonpython calculator onlinewriting a calculator on pythonpython program for calculatorpython function calculatormake calculator in pythonpython making a calculatorhow to make function calculator in pythonhow to calculat in pythonhow to have a calculating input in pythonmake calculator using pythonhow to make a calculator in pythonhow to make a calculator in python