0

Here is my code

how do create a method that allows me to change the postcode ?

supplier_105 = {
    "city": "Port Bradley",
    "contact_firstname": "Brittany",
    "contact_lastname": "Costa",
    "contact_title": "Mrs",
    "country": "Australia",
    "email": "brittany8706.costa@gmail.com",
    "notes": "",
    "phone": "(08) 6939 8022",
    "postcode": "3880",
    "state": "Costa",
    "street_address": "6/81 Heather Rosebowl",
    "supplier_id": 105,
    "supplier_name": "Rodriguez, Carter and Johnson",
}


# Write your code here

class Supplier:
    def __init__(
        self,
        city,
        contact_firstname,
        contact_lastname,
        contact_title,
        country,
        email,
        notes,
        phone,
        postcode,
        state,
        street_address,
        supplier_id,
        supplier_name,
    ):
        print("Initialiser called")
        self.city = city
        self.contact_firstname = contact_firstname
        self.contact_lastname = contact_lastname
        self.contact_title = contact_title
        self.country = country
        self.email = email
        self.notes = notes
        self.phone = phone
        self.postcode = postcode
        self.state = state
        self.street_address = street_address
        self.supplier_id = supplier_id
        self.supplier_name = supplier_name

    def get_state(self):
        return "The state is {} and the postcode is {}".format(self.state, self.postcode)


# 2. Instantiate the class
Supplier_105 = Supplier(
    "Port Bradley",
    "Brittany",
    "Costa",
    "Mrs",
    "Australia",
    "brittany8706.costa@gmail.com",
    "",
    "(08) 6939 8022",
    "3880",
    "Costa",
    "6/81 Heather Rosebowl",
    "105",
    "Rodriguez, Carter and Johnson",
)
print(Supplier_105.supplier_name)

# 3. Call its methods here
print(Supplier_105.get_state())

How do i set the postcode in the instance variable to the value of the parameter passed. i.e how do create a method that allows me to change the postcode ? The postcode is 3880, how do i change it to 4980 by the use of a method.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    The title mentions a list. The `postcode` isn't in a list. – Barmar Aug 17 '21 at 19:03
  • 1
    Does this answer your question? [What's the pythonic way to use getters and setters?](https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters) – jkr Aug 17 '21 at 19:21

1 Answers1

0

Add this method to your class:

def set_postcode(self, postcode):
    self.postcode = postcode

Then you can use:

Supplier_105.set_postcode('4980')

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612