Skip to content

Commit 4f9bc94

Browse files
authored
Add files via upload
A simple calculator built using OOP, classes and functions
1 parent 52ecd29 commit 4f9bc94

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class calculate:
2+
def __init__(self, first_number, second_number):
3+
4+
self.first_numbeer = first_number
5+
self.second_number = second_number
6+
7+
8+
9+
def add(self):
10+
return self.first_numbeer + self.second_number
11+
12+
def mul(self):
13+
return self.first_numbeer * self.second_number
14+
15+
def sub(self):
16+
return self.first_numbeer - self.second_number
17+
18+
def div(self):
19+
if self.second_number != 0:
20+
return self.first_numbeer / self.second_number
21+
else:
22+
return f'Division by zero is invalid...'
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
from Calculator import calculate # import a class module I wrote in Calculator.py called calculate
2+
3+
signs = { # A dictionary to store signs according to user choices from menu
4+
1:'+',
5+
2:'-',
6+
3:'x',
7+
4:'/'
8+
}
9+
10+
def display_menu(): # A method to display menu options to users
11+
print(
12+
'''---Menu---
13+
1. Addition
14+
2. Subtraction
15+
3. Multiplication
16+
4. Division
17+
5. Quit
18+
19+
'''
20+
)
21+
accept_user_choice() # Calling a function that accepts user menu choices.
22+
23+
def accept_user_choice(): # A Method to accept user choices
24+
25+
user_choice = input('Enter a choice from (1 - 5): ') # a prompt variable to accept user choice and store them as string
26+
27+
if user_choice.isdigit(): # a condition to check if the user choice is a digit or not.
28+
user_choice = int(user_choice) # converting user choices from str to int after being a digit
29+
validate_user_choice(user_choice) # calling the validation method to validate if user choice is true.
30+
31+
else: # An else statement to prompt errors
32+
print('Invalid user choice...')
33+
accept_user_choice() # using recursion method to run this method to re-accept the user choice till conditions are met
34+
35+
36+
37+
38+
def validate_user_choice(choice): # a method to validate user choices for the menu options
39+
if 0 < choice < 5: # choices from users should be from (1 - 5) but 5 is an exception to quit the program
40+
display_results(choice) # display results after conditions are met
41+
42+
43+
elif choice == 5: # quit the program if a uer choice is 5
44+
print('Quiting...')
45+
quit()
46+
47+
else: # a prompt condition to alert users to enter a choice from 1-5
48+
print('Choice must be from 1 - 5')
49+
accept_user_choice() # calling this method to accept new user choices
50+
51+
def assign_user_choice_to_menu_option(x, y,choice): # a method to assign various choices to appropriate calculations
52+
53+
cal = calculate(x, y) # creating an object for the class calculate to be used in calling various class methods.
54+
55+
if choice == 1:
56+
return cal.add()
57+
elif choice == 2:
58+
return cal.sub()
59+
elif choice == 3:
60+
return cal.mul()
61+
else:
62+
return cal.div()
63+
64+
65+
def accept_values(): # A method to accept user values for calculations
66+
67+
x = input('Enter value 1: ')
68+
y = input('Enter value 2: ')
69+
70+
if (x and y).isdigit(): # Checking if valuses are digit then it's converted into float
71+
x = float(x)
72+
y = float(y)
73+
74+
else: # a case where values are not digits
75+
print('Please enter a digit / a number... ')
76+
accept_values()
77+
return x, y
78+
79+
def display_results(choice):# method to display the answer
80+
x, y = accept_values()
81+
82+
answer = assign_user_choice_to_menu_option(x, y, choice) # calling this method to assign user choice to right menu option
83+
84+
for key, pair in signs.items(): # using a for loop to print the answer with the appropriate sign
85+
if key == choice:
86+
print(f'{x} {pair} {y} = {answer}')
87+
88+
89+
90+
91+
92+
def main(): # main loop
93+
print('--Calculator--')
94+
while True:
95+
display_menu() # calling this function to start the process
96+
97+
again = input('Do you want to continue (n / y): ').lower() # a prompt to ask users for continuation
98+
if again != 'y':
99+
quit()
100+
break
101+
else:
102+
print(' ')
103+
104+
105+
if __name__ == '__main__': # running from the mainloop...
106+
main()
107+
108+
#mMabiaa

0 commit comments

Comments
 (0)