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

calculator with python

#Simple python Calculator
#Enter the first variable
#then the operator
#and finally the second variable, then the answer is printed.
A = int(input("Enter your first number: "))
operator = input("Which operation would you like to perform? (+, -, /, *, //, **): ");
B = int(input("Enter your second number: "))
#Addition
if operator == "+":
    ans = A + B
    print(str(ans))
#Subraction
elif operator == "-":
    ans = A - B
    print(str(ans))
#division
elif operator == "/":
    ans = A / B
    print(str(ans))
#Multiplication
elif operator == "*":
    ans = A * B
    print(str(ans))
#Integer division
elif operator == "//":
    ans = A // B
    print(str(ans))
#Exponent
elif operator == "**":
    ans = A ** B
    print(str(ans))
else:
    print("wrong operator!")
Posted by: Guest on April-21-2021
0

python calculator

# simple calculator
try:
  # prompt user
    num1 = float(input("Please Enter The first number: "))
    operator = input("Please choose the operator (+) for addition, (-) for subtraction, (*) for multiplication"
                     "(/) for division , (%) for remainder: ")
    num2 = float(input("Please Enter The second number: "))
    
    if operator == '+':
        result = num1 + num2
        print("The result is: ", result)
    elif operator == '-':
        result = num1 - num2
        print("The result is: ", result)
    elif operator == '*':
        result = num1 * num2
        print("The result is: ", result)
    elif operator == '/':
        try:
            if True:
                result = num1 / num2
                print(result)
        except ZeroDivisionError as err:
            print(err, " oops! zero division occur ")
    elif operator == '%':
        if operator == '%':
            result = num1 % num2
            print("The result is: ", result)
    else:
        raise TypeError


except ValueError:
    print("wrong value, suggest integer or decimal")
finally:
    print("All Done")
Posted by: Guest on September-23-2020

Code answers related to "code for a calculator in python"

Python Answers by Framework

Browse Popular Code Answers by Language