-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator-python.py
76 lines (61 loc) · 1.89 KB
/
calculator-python.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
import math
def display_options():
print("Hello User, this is a calculator made by Python!")
print("The following operations are available: ")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Power (^)")
print("6. Modulus (%)")
print("7. Logarithm (log)")
def addition(x, y):
return x + y
def subtraction(x, y):
return x - y
def multiplication(x, y):
return x * y
def division(x, y):
if y != 0:
return x / y
else:
return "Error: Division by zero."
def power(x, y):
return x ** y
def modulus(x, y):
return x % y
def logarithm(x, base):
if x > 0 and base > 0 and base != 1:
return math.log(x, base)
else:
return "Error: Invalid input for logarithm."
def calculator():
while True:
display_options()
option = input("Enter the symbol of a mathematical operation or 'q' to quit: ")
if option == 'q':
print("Exiting the calculator. Goodbye!")
break
num1 = float(input("Enter the first number: "))
if option != 'log':
num2 = float(input("Enter the second number: "))
if option == '+':
result = addition(num1, num2)
elif option == '-':
result = subtraction(num1, num2)
elif option == '*':
result = multiplication(num1, num2)
elif option == '/':
result = division(num1, num2)
elif option == '^':
result = power(num1, num2)
elif option == '%':
result = modulus(num1, num2)
elif option == 'log':
base = float(input("Enter the base for the logarithm: "))
result = logarithm(num1, base)
else:
result = "Invalid input"
print(f"The result is: {result}")
if __name__ == "__main__":
calculator()