Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:22:28+00:00 2026-06-17T22:22:28+00:00

I wand to build a class SwitchBoard with the following properties: when I create

  • 0

I wand to build a class SwitchBoard with the following properties:

  • when I create a switchboard, I should be able to set the number of switches it contains
    all switches should start in the “off” position

  • if I print a switchboard, it should print something along the lines of: “The following switches are on: 1 3 5 7 9”

  • if I call switch(n) with n as an integer, it should ip the status of the n’th lightswitch

  • if I call switch every(n) with n as an integer, it should ip the status of every n’th lightswitch

I should be able to write a piece of global code that solves the lightswitch problem as stated above.

This is my work so far, the first is class LightSwitch

  • If switch is ON, it will flip and turn it OFF

  • If switch is OFF, it will flip and turn it ON

class LightSwitch():

    def __init__(self, mode):
            self.switch = mode

    def __str__(self):
        if self.switch == True:
            return ("I am on")

        if self.switch == False:
            return ("I am off")        

    def turn_on(self):
        self.switch = True

    def turn_off(self):
        self.switch = False

    def flip(self):
        if self.switch == True:
            self.switch = False
        else:
            self.switch = True

And for the class SwitchBoard, this is what I have:

class SwitchBoard():

    def __init__(self, n):
        self.switches = []
        for i in range(n):
            self.switches.append(LightSwitch(False))

    def switch(self, n):
        self.switches[n].flip()

    def switch_every(self, n):
        for i in range(0,len(self.switches), n):
            switch(i)

    def __str__(self):
        return "The following switches are on: " + str(self.switches)


if __name__ == ('__main__'):

    my_light = LightSwitch(False)
    my_light.flip()
    print (my_light) # This prints "I am on"

    my_light2 = LightSwitch(True)
    my_light2.flip()
    print(my_light2) # This prints "I am off"

    my_switchboard = SwitchBoard(10) # This is the issue.
    my_switchboard.switch_every(2)
    print (my_switchboard)

This isn’t working out, I’m using a list to append the number of OFF switches but I’m not sure how to write global code to solve this issue, such as turning on switches within a sequence, or maybe something is missing, please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:22:29+00:00Added an answer on June 17, 2026 at 10:22 pm

    To call the method switch, use self.switch(i) not switch(i):

    def switch_every(self, n):
        for i in range(0,len(self.switches), n):
            self.switch(i)   # <-- Change here
    

    To get some readable output when you call

     print (my_switchboard)     
    

    change the LightSwitch.__str__ method to LightSwitch.__repr__:

     def __repr__(self):   # <-- Change `__str__` to `__repr__`
         if self.switch == True:
             return "I am on"
    
         if self.switch == False:
             return "I am off"
    

    This is because, when you print a list, the repr of its contents are printed.


    The LightSwitch.flip method could be shortened to:

     def flip(self):
         self.switch = False if self.switch else True
    

    using a conditional expression.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have application how open Tshark process and start capturing packet, this process create
I just start to develop for windows phone. I wand to convert my apps
I wand build project in windows, I get same Errors: bash C:\linux\android-ndk1\ndk-build V=1 cygwin
I have two tables and I wand to create a junction table. I have
how to query a table Store(ID,Title,Company) to get all the records where title contains
For example: select name from person where id=2; I wand to know if this
My query: mysql::getInstance()->update('requests', array('response' => mysql_real_escape_string($_POST['status'])), array('secret' => $_POST['secret'])); ?> If i wand to
Today I received a crash report because in the following snippet w and/or h
I have an XML resource file for an Android App that contains many string
I have, as an example, the following two images: http://img571.imageshack.us/i/spain.jpg/ http://img97.imageshack.us/i/spainoverlay.png/ I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.