0

We have a loud talking parrot. The hour parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return True if we are in trouble.

parrot_trouble(True, 6) → True
parrot_trouble(True, 7) → False
parrot_trouble(False, 6) → False

This is my code

def parrot_trouble(talking, hour):
    if talking and hour < 7 or hour > 20:
        return True
    else:
        return False

my code has solve most of the problems but these last two are not working please help!

parrot_trouble(False, 21) → False
parrot_trouble(False, 23) → False

Also if you have a more efficient code please do write it.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Albert
  • 3
  • 1

2 Answers2

1

Add parenthesis to hour < 7 or hour > 20. otherwise when hours>20 ,talking and hour < 7 or hour > 20 get true.

def parrot_trouble(talking, hour):
    if talking and (hour < 7 or hour > 20):
        return True
    else:
        return False
print(parrot_trouble(False, 21))

Edit: Also, you can use (As in the first comment),

def parrot_trouble(talking, hour): 
    return talking and (hour < 7 or hour > 20)
Prasad Darshana
  • 186
  • 2
  • 14
  • You can even write it like this : def parrot_trouble(talking, hour): return talking and (hour < 7 or hour > 20): return True – Jib'z Oct 14 '21 at 14:45
0

You could break out the condition to immediately return False if it's not talking

def parrot_trouble(talking, hour):
    if not talking:
        return False
    return hour < 7 or hour > 20
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245