Currency Converter

Challenge: Currency Converter Class with Accessors & Mutators

Objective: Create a CurrencyConverter class that converts an amount from a foreign currency to your local currency, using accessor and mutator methods.


1. Class Properties (Instance Variables)

  • currency: The name of the foreign currency (e.g., “USD”, “AUD”).
  • rate: The conversion rate of that currency to your local currency (e.g., 1 USD = 70 INR).

2. Class Methods

  • __init__(self, currency, rate)
    • The constructor that takes the initial currency name and conversion rate.
    • Initializes the object’s properties with these values.
  • Accessor Methods (Getters)
    • get_currency(self)
      • Returns the current currency name.
    • get_rate(self)
      • Returns the current conversion rate.
  • Mutator Methods (Setters)
    • set_currency(self, new_currency)
      • Changes the currency name to new_currency.
    • set_rate(self, new_rate)
      • Changes the conversion rate to new_rate.
  • Core Functionality Method
    • convert(self, amount)
      • Takes an amount in the foreign currency.
      • Formula: amount * rate
      • Returns the converted value in your local currency.

3. Task Instructions

  1. Write the CurrencyConverter class with the constructor, two getters, two setters, and the convert method.
  2. Create an object of the class (e.g., for “USD” with a rate of 70).
  3. Test the class by:
    • Using the convert method to convert an amount (e.g., 100 USD).
    • Using the setter methods to change the currency and rate (e.g., to “AUD” with a rate of 50).
    • Using the getter methods to check the new currency and rate.
    • Calling the convert method again with a new amount to verify the updated conversion works.

class CurrencyConverter:

    def __init__(self, name, rate):
        self.currency = name
        self.rate = rate

    def get_currency(self):
        return self.currency

    def get_rate(self):
        return self.rate

    def set_currency(self, name):
        self.currency = name

    def set_rate(self, rate):
        self.rate = rate

    def convert(self, amount):
        return self.currency + ' conversion is ' + str(self.rate * amount)


cc = CurrencyConverter('USD', 70)
print(cc.convert(100))

cc.set_currency('AUD')
cc.set_rate(50)

print(cc.convert(100))

Similar Posts

  • Functions as Parameters in Python

    Functions as Parameters in Python In Python, functions are first-class objects, which means they can be: Basic Concept When we pass a function as a parameter, we’re essentially allowing one function to use another function’s behavior. Simple Examples Example 1: Basic Function as Parameter python def greet(name): return f”Hello, {name}!” def farewell(name): return f”Goodbye, {name}!” def…

  • Python Installation Guide: Easy Steps for Windows, macOS, and Linux

    Installing Python is a straightforward process, and it can be done on various operating systems like Windows, macOS, and Linux. Below are step-by-step instructions for installing Python on each platform. 1. Installing Python on Windows Step 1: Download Python Step 2: Run the Installer Step 3: Verify Installation If Python is installed correctly, it will…

  • re module

    The re module is Python’s built-in module for regular expressions (regex). It provides functions and methods to work with strings using pattern matching, allowing you to search, extract, replace, and split text based on complex patterns. Key Functions in the re Module 1. Searching and Matching python import re text = “The quick brown fox jumps over the lazy dog” # re.search()…

  • Thonny: A User-Friendly Python IDE for Beginners in 2025

    Thonny is a free and open-source Integrated Development Environment (IDE) specifically designed for beginners learning Python. It provides a simple and user-friendly interface, making it an excellent choice for those new to programming. Key Features: Why Thonny is good for beginners: How to install Thonny: If you’re new to Python and looking for a user-friendly…

  • file properties and methods

    1. file.closed – Is the file door shut? Think of a file like a door. file.closed tells you if the door is open or closed. python # Open the file (open the door) f = open(“test.txt”, “w”) f.write(“Hello!”) print(f.closed) # Output: False (door is open) # Close the file (close the door) f.close() print(f.closed) # Output: True (door is…

  • Predefined Character Classes

    Predefined Character Classes Pattern Description Equivalent . Matches any character except newline \d Matches any digit [0-9] \D Matches any non-digit [^0-9] \w Matches any word character [a-zA-Z0-9_] \W Matches any non-word character [^a-zA-Z0-9_] \s Matches any whitespace character [ \t\n\r\f\v] \S Matches any non-whitespace character [^ \t\n\r\f\v] 1. Literal Character a Matches: The exact character…

Leave a Reply

Your email address will not be published. Required fields are marked *