Skip to content

This repository contains an interactive calculator program written in Python. The program allows users to perform basic arithmetic operations by entering an operation and two numbers. The result is calculated and displayed. Users have the option to try another calculation or exit the program.

Notifications You must be signed in to change notification settings

aieckxis/calculator-inheritance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Object-Oriented Calculator with Inheritance in Python

This repository contains a Python script for an interactive calculator program that allows users to perform basic arithmetic operations that applies inheritance. It asks the user to enter an operation (+, -, *, /, power) and two numbers. The program then calculates the result and prints it. After each calculation, the user is asked if they want to try again. If they choose to try again, they can perform another calculation. If they choose not to try again, the program exits with a "Thank you!" message.

Usage

Using Command Prompt:

  1. Open the Start menu and search for "Command Prompt".
  2. Click on "Command Prompt" to open the command prompt window.
  3. Use the cd command to navigate to the directory containing the script file. For example, if the script file is located in the "Documents" folder, type: cd Documents
  4. Type the command python script_name.py to run the script. Replace "script_name.py" with the actual name of the main script file.
  5. Press Enter to run the script.
  6. The script will run and will display a prompt asking the user to choose a math operation (+, -, *, /, power).
  7. The program will ask the user to enter two numbers.
  8. The program will calculate the result based on the chosen operation and display it.
  9. If the user attempt to divide by zero, an error message will be shown, and user will be asked to enter the second number again.
  10. After displaying the result, the program will ask if the user want to try again.
  11. If the user choose to try again, they can input a new operation and numbers.
  12. The program will continue running until ther user choose to exit.

Alternatively, you can also run the script using the Python IDLE environment:

  1. Open the Start menu and search for "Python".
  2. Click on "Python" to open the Python IDLE environment.
  3. Click on "File" at the top of the window and select "Open".
  4. Navigate to the directory containing the main script file and select it.
  5. Click on "Run" at the top of the window and select "Run Module" or press the F5 key.
  6. The script will run and will display a prompt asking the user to choose a math operation (+, -, *, /, power).
  7. The program will ask the user to enter two numbers.
  8. The program will calculate the result based on the chosen operation and display it.
  9. If the user attempt to divide by zero, an error message will be shown, and user will be asked to enter the second number again.
  10. After displaying the result, the program will ask if the user want to try again.
  11. If the user choose to try again, they can input a new operation and numbers.
  12. The program will continue running until ther user choose to exit.

Code Explanation

— program.py

image

The code begins by importing the UserInterface, ExtendedUserInterface, and ask_try_again classes from separate Python files (user_interface.py, extended_user_interface.py, and try_again.py).

image

Next, the main() function is defined. Inside this function, instances of the UserInterface and ExtendedUserInterface classes are created and assigned to variables ui and eui, respectively.

image

The program enters a while loop, which allows the user to perform multiple calculations until they choose to exit. Within the loop, the program uses the ExtendedUserInterface to get the desired math operation and numbers from the user. The program then calculates the result based on the chosen operation using the calculate_result() method of ExtendedUserInterface. After calculating the result, the program prints it using the print_result() method of ExtendedUserInterface. The program asks the user if they want to try again by calling the ask_try_again() function from try_again.py. If the user chooses not to try again (input 'n'), the program breaks out of the loop and displays the message "Okay, thank you!".

image

Finally, the main() function is called to start the program if the script is executed directly.

— user_interface.py

image

The UserInterface class is defined with an _ _ init _ _ method. This method initializes the class attributes operation, num1, and num2 to default values.

image

The get_operation method is defined to ask the user for the desired math operation. It uses a while loop to repeatedly ask for input until a valid operation is entered. The valid operations are +, -, *, /, and power.

image

The get_numbers method is defined to ask the user for two numbers. It uses a try-except block to handle invalid inputs and continues to ask for input until two valid numbers are entered.

image

The calculate_result method is defined to perform the calculation based on the chosen operation. It uses a series of conditional statements (if, elif) to determine the operation and perform the corresponding calculation. The result is stored in the result attribute of the class.

image

The print_result method simply prints the result to the console.

— extended_user_interface.py

image

Imports the UserInterface class from the user_interface module. It allows the UserInterface class to be used in the current module.

image

The ExtendedUserInterface class inherits from the UserInterface class by specifying UserInterface in parentheses after the class name. The get_operation() method is overridden to provide a custom implementation. It asks the user to choose a math operation by taking input from the user and validates the input to ensure it is one of the valid operations: "+", "-", "*", "/", or "power".

image

The get_numbers() method is also overridden to provide a custom implementation. It asks the user to enter the first and second numbers, and performs input validation to ensure that valid floating-point numbers are entered. If an invalid input is provided, an error message is displayed.

image

The calculate_result() method overrides the parent class's method. It first checks if the chosen operation is division ("/") and if the second number is zero. If both conditions are met, it displays an error message indicating that division by zero is not allowed, asks the user to enter the second number again using the get_numbers() method, and returns from the method. If the conditions are not met, it calls the parent class's calculate_result() method using super().calculate_result() to perform the calculation based on the chosen operation.

image

The print_result() method overrides the parent class's method. It calls the parent class's print_result() method using super().print_result() to print the calculated result.

— try_again.py

image

The function is defined using the def keyword, followed by the function name ask_try_again(). It does not take any arguments and does not return any values.

image

Within the function, there is a while loop that continues until a valid input is received from the user. This loop ensures that the user is asked repeatedly until they provide a valid response. Inside the loop, the user is asked with the question "Do you want to try again? (y/n):". The input() function is used to capture the user's response, which is then converted to lowercase using the .lower() method. This conversion helps to make the comparison case-insensitive.

image

The user's input is then checked using an if statement to determine if it matches either "y" or "n". If the input is valid, meaning it is either "y" or "n", the function exits the loop using the return statement, and the user's response is returned as the result of the function. If the user enters an invalid input, any value other than "y" or "n", an error message is displayed using the print() function. This informs the user that their input is invalid and prompts them to enter either "y" or "n" again. The function continues to loop until a valid input is provided by the user, ensuring that the program does not proceed until a valid choice is made.

About

This repository contains an interactive calculator program written in Python. The program allows users to perform basic arithmetic operations by entering an operation and two numbers. The result is calculated and displayed. Users have the option to try another calculation or exit the program.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages