-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuBasedCalculator.py
More file actions
88 lines (68 loc) · 2.24 KB
/
MenuBasedCalculator.py
File metadata and controls
88 lines (68 loc) · 2.24 KB
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
################################
# Author: Rahim Siddiq
# Menu Based Calculator
################################
def add(): # Function Add
return n1 + n2
def subtract(): # Function Subtract
return n1 - n2
def multiply(): # Function Multiply
return n1 * n2
def divide(): # Function Divide
return n1 / n2
def remainder(): # Function Modulus / Remainder
return n1 % n2
def integer_division(): # Function Integer Division
return n1 // n2
def exponentiation(): # Function Exponentiation
return n1 ** n2
def average(): # Function Average
return sum(a) / n
print("Please select the number of the function you wish to calculate:")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Remainder")
print("6.Integer_Division")
print("7.Exponentiation")
print("8.Average")
cfunction = input("Enter Number of The Function: ")
if cfunction in ("1","2","3","4","5","6","7"):
n1 = float(input("Enter first number: "))
n2 = float(input("Enter second number: "))
if cfunction == "1":
print(n1, "+", n2, "=", add())
elif cfunction == "2":
print(n1, "-", n2, "=", subtract())
elif cfunction == "3":
print(n1, "*", n2, "=", multiply())
elif cfunction == "4":
if n2 == 0:
print("Error: cannot divide by zero")
else:
print(n1, "/", n2, "=", divide())
elif cfunction == "5":
if n2 == 0:
print("Error: modulus by zero is undefined")
else:
print(n1, "%", n2, "=", remainder())
elif cfunction == "6":
if n2 == 0:
print("Error: integer division by zero")
else:
print(n1, "//", n2, "=", integer_division())
elif cfunction == "7":
print(n1, "**", n2, "=", exponentiation())
elif cfunction == "8":
n = int(input("How many numbers would you like to average? "))
if n < 1:
print("Please enter a value of at least 1.")
else:
a = []
for i in range(n):
val = float(input("Enter a number: "))
a.append(val)
print("Values entered", list(a), "Average:", average())
else:
print("Please select the number of the function you would like to perform.")