I've been into computer science fundamentals (proper data structures/algorithms/etc) including Object Oriented Programming. Therefore, I decided to reformat my old traditional Python Calculator into an Object Oriented one.
import os
#Pre-Defined
def check_int(num):
try:
num = int(num)
return True
except:
return False
class User:
def __init__(self, username) -> None:
self.name = username
Calculator.main_page(username)
class Calculator:
def __init__(self) -> None:
pass
def main_page(username):
print(f'Welcome to The Python Calculator {username}!')
print('')
print('Please type the number of the operation you wish to do:')
print('1 -> Addition')
print('2 -> Substraction')
print('3 -> Multiplication')
#Operation Selection
user_input = input('')
while check_int(user_input) != True:
print('Error! Please give a valid response!')
user_input = input('')
user_input = int(user_input)
#Interfaces
if user_input == 1: #ADDITION
print("Please type in the numbers you wish to add, type x when complete!")
nums = []
while True: #Wait for x input
number_in = input('')
if number_in.lower() == "x": #Check for x
break
while check_int(number_in) != True: #Check Int
print('Number not registered, please type a valid number!')
number_in = input('')
number_in = int(number_in)
nums.append(number_in)
print(f'Your results are: {Calculator.addition(nums)}')
Calculator.end_screen(username)
def end_screen(username):
print("Thank you, would you like to use the calculator again? (yes/no)")
end_user_input = input('')
if end_user_input.lower() == "yes":
Calculator.main_page(username)
elif end_user_input.lower() == "no":
os.system('CLS' if os.name == 'nt' else 'clear')
print('Thank you for using the calculator, have a good day!')
else:
print("I'm sorry, unrecognized input, you will be redirected to the main page!")
print("")
Calculator.main_page(username)
#Processes
def addition(data):
res = sum(data)
return res
#Main Driver
if __name__ == '__main__':
os.system('CLS' if os.name == 'nt' else 'clear')
#Input username and instantiate
username = input('Please type your name: ')
os.system('CLS' if os.name == 'nt' else 'clear')
user = User(username)
So far I have only recreated the addition part yet, but I would like to know if the algorithm/code I've written is considered optimal/efficient. I would love to get some feedbacks about what could I improve in general.
def clear: os.system('CLS' if os.name == 'nt' else 'clear')
. \$\endgroup\$