Thats my first application on Python. Please help me do my code better. I try make the simple application . I don't know is I do it right and how do my code cleaner.
main.py . Entry point of my calculator where i just call run() method from conductor.py module.
import conductor
def main():
conductor.run()
if __name__ == '__main__':
main()
conductor.py . It is module where I hold methods for communication my modules , something like middleware (if I may say so)
import dialog
import operations
variables_list = []
def run():
dialog.print_operations_list()
processing()
def processing():
operation = dialog.get_operation()
result = dialog.get_result
if operation == '1':
result(operations.addition(get_many_variables()))
elif operation == '2':
result(operations.subtract(get_many_variables()))
elif operation == '3':
result(operations.multiply(get_many_variables()))
elif operation == '4':
result(operations.divide(get_many_variables()))
elif operation == '5':
result(operations.exponent(get_one_variable()))
elif operation == '6':
result(operations.square(get_one_variable()))
elif operation == '7':
result(operations.cos(get_one_variable()))
elif operation == '8':
result(operations.sin(get_one_variable()))
elif operation == '9':
result(operations.tan(get_one_variable()))
elif operation == '0':
result(operations.cot(get_one_variable()))
else:
print('Invalid operation. Please try again!')
run()
def get_one_variable():
variable = dialog.get_variables()
if variable == '0':
print("You can't write zero, because ist provide an error")
return get_one_variable()
elif variable != '':
variables_list.append(int(variable))
return variables_list
else:
print("Invalid number")
get_one_variable()
def get_many_variables():
variable = dialog.get_variables()
if variable == '0':
print("You can't write zero, because ist provide an error")
return get_many_variables()
elif variable != '':
variables_list.append(int(variable))
return get_many_variables()
else:
if len(variables_list) <= 1:
print('Invalid numbers. You must write minimum 2 numbers')
return get_many_variables()
else:
return variables_list
dialog.py This is module who provide communication with user by input or output some console messages.
def greetings():
print("Hi, I'm Calculator.\n")
def print_operations_list():
print("At this moment you can: \n "
"Additional, write 1 \n "
"Subtract, write 2\n "
"Multiply, write 3\n "
"Divide, write 4\n "
"Exponent, write 5\n "
"Square, write 6\n "
"Cosines, write 7\n "
"Sinus, write 8\n "
"Tangents, write 9\n "
"Cotangents, write 0\n ")
def get_operation():
return input('Write what do you want to do? \n')
def get_variables():
return input('Write number... \n')
def get_result(result):
print(f"Your result is: {result}")
operation.py This module consist of mathematical operation methods, and northing more
import math
def addition(variables):
result = variables[0]
for variable in variables[1:]:
result += variable
return result
def subtract(variables):
result = variables[0]
for variable in variables[1:]:
result -= variable
return result
def multiply(variables):
result = variables[0]
for variable in variables[1:]:
result *= variable
return result
def divide(variables):
result = variables[0]
for variable in variables[1:]:
result /= variable
return result
def exponent(variables):
return variables[0] ** 2
def square(variables):
return math.sqrt(variables[0])
def cos(variables):
return math.cos(variables[0])
def sin(variables):
return math.sin(variables[0])
def tan(variables):
return math.tan(variables[0])
def cot(variables):
return math.cos(variables[0]) / math.sin(variables[0])
()
afterdialog.get_result
\$\endgroup\$