0

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".

My code:

  def alarm_clock(day,vacation):
  if(vacation):
      if(day == 0 | day == 6):
          return "off"
      return "10:00"
  else:
      if(day == 0 | day == 6):
          return "10:00"
      return "7:00"

With input:

print(alarm_clock(0,True))

My code returns '10:00' when it should be 'off'

With input:

print(alarm_clock(0,False))

My code returns '7:00', it should be '10:00'

Where is the bug in my code?

sniperd
  • 5,124
  • 6
  • 28
  • 44
J.Woods
  • 25
  • 6

3 Answers3

1

Change it like this:

def alarm_clock(day,vacation):
    if(vacation):
        if(day == 0 or day == 6):
            return "off"
        return "10:00"
    else:
        if(day == 0 or day == 6):
            return "10:00"
        return "7:00"

print(alarm_clock(0,True))

results:

off

The pipe isn't doing what you'd expect: Pipe character in Python it is in fact a bitwise operator. :)

sniperd
  • 5,124
  • 6
  • 28
  • 44
0

As you can see in the official documentation, the logical "or" operator in Python is or, not | which is a bitwise operator

Hearner
  • 2,711
  • 3
  • 17
  • 34
0

You are using a bitwise or "|". You need to use a logical or "or"

def alarm_clock(day,vacation):
  if(vacation):
      if(day == 0 or day == 6):
          return "off"
      return "10:00"
  else:
      if(day == 0 or day == 6):
          return "10:00"
      return "7:00"
AzaDee
  • 85
  • 7