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()
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
1#My Personal Python calculator
2print("My Personal Python calculator")
3#inputs
4num1 = int(input('Enter the first number: '))
5num2 = int(input('Enter the second number: '))
6#opration req
7opr = input('Enter the type of operation you want to perform between your chosen two numbers: ')
8#calculation
9if opr=='+':
10 ans = num1 + num2
11 # displaying the answer
12 print(f'Your final answer is {ans}')
13elif opr == '- ':
14 ans = num1 - num2
15 # displaying the answer
16 print(f'Your final answer is {ans}')
17elif opr=='*':
18 ans = num1 * num2;
19 # displaying the answer
20 print(f'Your final answer is {ans}')
21elif opr=='/':
22 ans = num1 / num2
23 # displaying the answer
24 print(f'Your final answer is {ans}')
25else:
26 print('Invalid Entry!!!')
1Select operation.
21.Add
32.Subtract
43.Multiply
54.Divide
6Enter choice(1/2/3/4): 3
7Enter first number: 15
8Enter second number: 14
915.0 * 14.0 = 210.0
10
1def calculate():
2 operation = input('''
3Please type in the math operation you would like to complete:
4+ for addition
5- for subtraction
6* for multiplication
7/ for division
8''')
9
10 number_1 = int(input('Please enter the first number: '))
11 number_2 = int(input('Please enter the second number: '))
12
13 if operation == '+':
14 print('{} + {} = '.format(number_1, number_2))
15 print(number_1 + number_2)
16
17 elif operation == '-':
18 print('{} - {} = '.format(number_1, number_2))
19 print(number_1 - number_2)
20
21 elif operation == '*':
22 print('{} * {} = '.format(number_1, number_2))
23 print(number_1 * number_2)
24
25 elif operation == '/':
26 print('{} / {} = '.format(number_1, number_2))
27 print(number_1 / number_2)
28
29 else:
30 print('You have not typed a valid operator, please run the program again.')
31
32 # Add again() function to calculate() function
33 again()
34
35def again():
36 calc_again = input('''
37Do you want to calculate again?
38Please type Y for YES or N for NO.
39''')
40
41 if calc_again.upper() == 'Y':
42 calculate()
43 elif calc_again.upper() == 'N':
44 print('See you later.')
45 else:
46 again()
47
48calculate()