Answers for "build a calculator using 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
1

create calculator in python

from tkinter import *
import random
import time

def btnClick(numbers):
	global operator
	operator = operator + str(numbers)
	text_Input.set(operator)

def bcd():
	global operator
	operator = ""
	text_Input.set("")

def bei():
	global operator
	sumup = str(eval(operator))
	text_Input.set(sumup)
	operator = sumup


root = Tk()
root.title("Calculator")
root.resizable(False, False)

operator = ""
text_Input = StringVar()

#AnsShow
textDisplay = Entry(root, font = ('arial', 20, 'bold'), textvariable = text_Input, bd = 30, insertwidth = 4, bg = "red", justify = 'right')
textDisplay.grid(columnspan = 4)

#button
btn7 = 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)

btn8 = 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)

btn9 = 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)

Add = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "+", command = lambda:btnClick("+")).grid(row = 1, column = 3)

#============================================================================================================================#
btn4 = 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)

btn5 = 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)

btn6 = 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)

Sub = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "-", command = lambda:btnClick("-")).grid(row = 2, column = 3)

#===============================================================================================================================#
btn1 = 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)

btn2 = 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)

btn3 = 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)

Multiply = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "*", command = lambda:btnClick("*")).grid(row = 3, column = 3)

#==================================================================================================================================#
btn0 = 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)

equal = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "=", command = bei).grid(row = 4, column = 1)

divide = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "/", command = lambda:btnClick("/")).grid(row = 4, column = 2)

clear = Button(root, padx = 16, bd = 8, fg = "black", bg = "red", font = ('arial', 20, 'bold'), text = "c", command = bcd).grid(row = 4, column = 3)

root.mainloop()
Posted by: Guest on August-17-2021
1

python calculator

print("Enter Your Choice 1(Add)/2(Sub)/3(Divide)/4(Multiply)")
num = int(input())
if num == 1:
    print("Enter Number 1 : ")
    add1  = int(input())
    print("Enter Number 2 : ")
    add2 = int(input())
    sum = add1 + add2
    print("The Sum Is ", sum)
elif num == 2:
    print("Enter Number 1 : ")
    sub1  = int(input())
    print("Enter Number 2 : ")
    sub2 = int(input())
    difference = sub1 - sub2
    print("The Difference Is ", difference)
elif num == 3:
    print("Enter Number 1 : ")
    div1  = float(input())
    print("Enter Number 2 : ")
    div2 = float(input())
    division = div1 / div2
    print("The Division Is ", division)
elif num == 4:
    print("Enter Number 1 : ")
    mul1 = int(input())
    print("Enter Number 2 : ")
    mul2 = int(input())
    multiply = mul1 * mul2
    print("The Difference Is ", multiply)
else:
    print("enter a valid Number")
Posted by: Guest on December-26-2020
0

Calculator in python

num1 = input("Enter a Number : ")
num2 = input("Enter a Number : ")
result = (num1 * num2)
print(result)
# And then print out the result
Posted by: Guest on August-21-2020
0

calculator python

#this is a fraction calculator
def fractions_regular(fractions):
    f1 = input(13,25)
    f1 = fractions.Fraction(f1)
    f2 = input(6,25)
    f2 = fractions.Fraction(f2)
    operation = input('what is your iperation?')
    if operation == '+':
        print('{} + {} = {}'.format(f1, f2, f1 + f2))
    if operation == "-":
        print('{} - {} = {}'.format(f1, f2, f1 - f2))
    if operation == "*":
        print('{} * {} = {}'.format(f1, f2, f1 * f2))
    if operation == "/":
        print('{} / {} = {}'.format(f1, f2, f1 / f2))
Posted by: Guest on March-25-2021

Code answers related to "build a calculator using python"

Python Answers by Framework

Browse Popular Code Answers by Language