0

Does this run on your pc with pycharm?

#Written by Jamie Peacock, Basic calculator program # works perfectly on pycharm so if this helps just copy over # insert breakpoint on line one and debug , use F8 key to follow step by step class Calculator: def add(self): num1 = int(input(''' Enter first number: ''')) num2 = int(input(''' Enter second number: ''')) result = num1 + num2 print(''' The sum is ''' + str(result)) def sub(self): num1 = int(input(''' Enter first number: ''')) num2 = int(input(''' Enter second number: ''')) result = num1 - num2 print(''' The subtraction is ''' + str(result)) def multi(self): num1 = int(input(''' Enter first number: ''')) num2 = int(input(''' Enter second number: ''')) result = num1 * num2 print(''' The multiplication is ''' + str(result)) def div(self): num1 = int(input(''' Enter first number: ''')) num2 = int(input(''' Enter second number: ''')) result = num1 / num2 print(''' The division is ''' + str(result)) c1 = Calculator() while True: user_input = input('Do you want to start a calculation y or n: ') if user_input.lower() == 'y': print(''' select an operation to perform ''') print('1. ADD') print('2. SUBTRACT') print('3. MULTIPLY') print('4. DIVIDE') operation = int(input(''' Enter selection of operator: ''')) if operation == 1: c1.add() elif operation == 2: c1.sub() elif operation == 3: c1.multi() elif operation == 4: c1.div() elif operation >= 5: print(''' option not available''') else: break

28th Feb 2022, 8:51 PM
Jamie Peacock
4 Answers
+ 1
Whenever we write a code and we see that we are repeating things up, that's the time we consider making a method to handle the repetitive parts. I'm referring to the number input parts which is repeated in each operation (add, sub etc.)
1st Mar 2022, 12:19 AM
Ipang
0
Yes,You can
28th Feb 2022, 8:56 PM
Beary
Beary - avatar
0
Good job. Something to try adding in for some extra practice: 1) Python console clearing to keep a clean interface 2) Ability to keep result and continue with it further. For example, 5 + 5 = 10, now you can use 10 and do another operation with it. 3) Add a custom error message for when a user tries to divide by 0 4) doing a class based calculator is cool, but can what about eliminating a lot of redundant code by passing in the values to each method rather than having each method ask the user for values Just a thought for some extra work! Good job
28th Feb 2022, 10:49 PM
ForgetfulMemoryFoam
ForgetfulMemoryFoam - avatar
0
Just as a follow up. I changed some of the code around myself and took what you had from 66 lines down to 42 lines. There can always be more taken off, but the majority of it was taking the inputs during the class init and then passing them into the functions. What do you get when you try this?
28th Feb 2022, 11:08 PM
ForgetfulMemoryFoam
ForgetfulMemoryFoam - avatar