{"id":167,"date":"2026-09-03T08:44:44","date_gmt":"2026-09-03T08:44:44","guid":{"rendered":"https:\/\/alpeshconnect.in\/blog\/?p=167"},"modified":"2026-09-03T08:45:40","modified_gmt":"2026-09-03T08:45:40","slug":"create-class-voter-and-check-eligibility","status":"publish","type":"post","link":"https:\/\/alpeshconnect.in\/blog\/2026\/09\/03\/create-class-voter-and-check-eligibility\/","title":{"rendered":"Create class voter and check eligibility"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Create class Voter <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Private variable : age<\/li>\n\n\n\n<li>Method : set_age(a) \u2192 sets age if a>=18<\/li>\n\n\n\n<li>Method : is_eligible() \u2192 returns True if age >= 18, else False<\/li>\n\n\n\n<li>Create an object, set age, and check eligibility<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Voter:\n    def __init__(self):\n        # Private variable (name mangling)\n        self.__age = 0\n\n    def set_age(self, age):\n        \"\"\"Sets the age if it is 18 or more.\"\"\"\n        if age &gt;= 18:\n            self.__age = age\n            print(f\"Age set to {self.__age}.\")\n        else:\n            print(f\"Error: Age {age} is less than 18. Age must be 18 or above.\")\n\n    def is_eligible(self):\n        \"\"\"Returns True if age is 18 or more, else False.\"\"\"\n        return self.__age &gt;= 18\n\n# --- Creating an object and checking eligibility ---\n\n# Object for a voter in India\nvoter1 = Voter()\n\nprint(\"--- Checking Eligibility for Voter 1 ---\")\n# Trying to set an ineligible age\nvoter1.set_age(17)\n# Checking eligibility\nprint(f\"Is eligible to vote? {voter1.is_eligible()}\")\n\nprint(\"\\n--- Checking Eligibility for Voter 2 ---\")\nvoter2 = Voter()\n# Setting a valid age (for a voter in Mumbai)\nvoter2.set_age(25)\nprint(f\"Is eligible to vote? {voter2.is_eligible()}\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create class Voter class Voter: def __init__(self): # Private variable (name mangling) self.__age = 0 def set_age(self, age): &#8220;&#8221;&#8221;Sets the age if it is 18 or more.&#8221;&#8221;&#8221; if age &gt;= 18: self.__age = age print(f&#8221;Age set to {self.__age}.&#8221;) else: print(f&#8221;Error: Age {age} is less than 18. Age must be 18 or above.&#8221;) def is_eligible(self): &#8220;&#8221;&#8221;Returns [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":96,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51,3],"tags":[52,25,26,45],"class_list":["post-167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aiopd","category-bca","tag-aiopd","tag-bca","tag-bca-paper-solution","tag-python"],"_links":{"self":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":1,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions\/168"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media\/96"}],"wp:attachment":[{"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alpeshconnect.in\/blog\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}