Hi
im having trouble with a lab that was assigned today. The program has take input from the user which lights are on button 1 controls the NW and SE hallways, button 2 controls the NE and SW hallways, button 3 controls the SE and NE hallways, and button 4 controls the SW and NW hallways the program then has too tell which lights are on. any hallway with 0 signal has the lights off, any hallway with 1 signal on has the lights on , and any hallway with 2 signals on has the lights off. and it all has to be done in a function using if and else statements.
I know that the when the user inputs data i need to use the raw_input("prompt") thing but after this im lost please help!

Member Avatar for masterofpuppets

i am not sure exactly what you want the program to do but here's what I came up with:

NW, SE, NE, SW = 0, 0, 0, 0
def checkLights( buttonNum ):
      global NW, SE, NE, SW
      if buttonNum == "button 1":
         NW += 1; SE += 1 #changed to += so that it's easier to check whether there are 2 'on' signals
      elif buttonNum == "button 2":
         NE += 1; SW += 1
      elif buttonNum == "button 3":
         SE += 1; NE += 1
      elif buttonNum == "button 4":
         SW += 1; NW += 1
      
      #this part would be easier with a for loop but since you need if..else ..
      if NW == 1:
         print "NW is on"
      if SE == 1:
         print "SE is on"
      if NE == 1:
         print "NE is on"
      if SW == 1:
         print "SW is on"

button = raw_input( "Enter button number ( e.g 'button 1' ) >> " )
checkLights( button )

this is just to start you off, you might want to modify the code inside the function to check whether a hallway has 2 'on' signals. :)

Thank you very much masterofpuppets

NW, SE, NE, SW = 0, 0, 0, 0
def checkLights( buttonNum ):
      global NW, SE, NE, SW
      if buttonNum == "button 1":

The use of global is`s not a god thing at all.
Try to avoid at all cost.
This do the same.

def checkLights( buttonNum ):
    NW, SE, NE, SW = 0, 0, 0, 0      
    if buttonNum == "button 1":
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.