-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
147 lines (118 loc) · 3.74 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import math
from datetime import datetime
# Ask user for an integer with the given prompt.
def getInt(prompt):
while True:
i = input(prompt)
try:
i = float(i)
except ValueError:
continue
return i
# Attempt to convert given value to an integer.
def tryToInt(i):
if i % 1 == 0:
return int(i)
else:
return i
# Log the given calculation.
def log(calculation):
now = datetime.now()
time = "[" + str(now.year) + "/" + str(now.month) + "/" + str(now.day) + " " + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second) + "]"
with open("log.txt", "a") as log:
log.write(time + " " + calculation + "\n")
# Receive and add two numbers.
def addition():
a = tryToInt(getInt("Enter a number: "))
b = tryToInt(getInt("Enter another number: "))
answer = a + b
calculation = str(a) + "+" + str(b) + " = " + str(tryToInt(answer))
print(calculation)
log(calculation)
input("")
# Receive and subtract two numbers.
def subtraction():
a = tryToInt(getInt("Enter a number: "))
b = tryToInt(getInt("Enter another number: "))
answer = a - b
calculation = str(a) + "-" + str(b) + " = " + str(tryToInt(answer))
print(calculation)
log(calculation)
input("")
# Receive and multiply two numbers.
def multiplication():
a = tryToInt(getInt("Enter a number: "))
b = tryToInt(getInt("Enter another number: "))
answer = a * b
calculation = str(a) + "*" + str(b) + " = " + str(tryToInt(answer))
print(calculation)
log(calculation)
input("")
# Receive and divide two numbers.
def division():
a = tryToInt(getInt("Enter a number: "))
b = tryToInt(getInt("Enter another number: "))
answer = a / b
calculation = str(a) + "/" + str(b) + " = " + str(tryToInt(answer))
print(calculation)
log(calculation)
input("")
# Receive and get the exponent of two numbers.
def exponentiation():
a = tryToInt(getInt("Enter a number: "))
b = tryToInt(getInt("Enter another number: "))
answer = a ** b
calculation = str(a) + "^" + str(b) + " = " + str(tryToInt(answer))
print(calculation)
log(calculation)
input("")
# Receive and get the square root of a number.
def squareroot():
a = tryToInt(getInt("Enter a number: "))
answer = math.sqrt(a)
calculation = "sqrt(" + str(a) + ")" + " = " + str(tryToInt(answer))
print(calculation)
log(calculation)
input("")
# Receive and get the factorial of a number.
def factorial():
while True:
a = tryToInt(getInt("Enter a number: "))
if a >= 0 and a % 1 == 0:
break
answer = math.factorial(a)
calculation = str(a) + "!" + " = " + str(tryToInt(answer))
print(calculation)
log(calculation)
input("")
# Prompt user for an operation to excecute.
def getOperation():
while True:
print("1) Addition")
print("2) Subtraction")
print("3) Multiplication")
print("4) Division")
print("5) Exponentiation")
print("6) Square Root")
print("7) Factorial")
while True:
operation = getInt("Operation: ")
if operation != None and operation % 1 == 0 and operation >= 1 and operation <= 7:
break
# Excecute given operation.
if operation == 1:
addition()
elif operation == 2:
subtraction()
elif operation == 3:
multiplication()
elif operation == 4:
division()
elif operation == 5:
exponentiation()
elif operation == 6:
squareroot()
elif operation == 7:
factorial()
# Launch calculator.
getOperation()