-1

Why is this code wrong :

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

And why is this code right:

def parrot_trouble(talking, hour):
  if talking == True and (hour < 7 or hour > 20):
    return True
  else : 
    return False
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
  • 2
    What do you mean by "right" and "wrong"? How do you call the function? What is the expected output? What is the actual output? And please read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 26 '18 at 10:53
  • well it's in codingbat warmup 1 parrot trouble if you could check it out that would be wonderful. – s0meth1ngg Oct 26 '18 at 10:56
  • Well i mean the output is different if i don't put the parenthesis in the if statement – s0meth1ngg Oct 26 '18 at 10:57
  • Related: [Priority of the logical statements NOT AND & OR in python](//stackoverflow.com/q/16679272) – Aran-Fey Oct 26 '18 at 11:03
  • 1
    Possible duplicate of [Priority of the logical statements NOT AND & OR in python](https://stackoverflow.com/questions/16679272/priority-of-the-logical-statements-not-and-or-in-python) – stovfl Oct 26 '18 at 13:39

7 Answers7

1

Look at the case parrot_trouble(False, 21):

It should return False, but it returns True in the first version.

And that's because first version of code is treated as:

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

First condition (in parenthesis) is not met here, but it doesn't matter as the second is met, and between them you use or. This won't happen in the second version, as when the parrot is not talking, we have no trouble.

Piotrek
  • 1,400
  • 9
  • 16
  • so the one in the parenthesis is evaluated first ? – s0meth1ngg Oct 26 '18 at 11:14
  • for the case `parrot_trouble(False, 21)` the code above gives you `False` in the first two conditions (in parenthesis) and `True` in the third. Having `False or True` will always return `True` and in your case the outcome should be `False` – Piotrek Oct 26 '18 at 11:18
  • and yes, without parenthesis it will 'look at' `and` first, then the result with `or` – Piotrek Oct 26 '18 at 11:20
  • `and` has a higher precedence than `or`, as pointed out in the link by @Aran-Fey – Piotrek Oct 26 '18 at 11:22
  • 1
    @Piotrek great answer, also @s0meth1ngg I would simplify a way the function is implemented: `def parrot_trouble(talking, hour): return talking and (hour < 7 or hour > 20)` – Dawid Fieluba Oct 28 '18 at 16:11
0

The first function is not necesarally wrong, it is just ambiguous. You have to know if the and or the or statement is evaluated first to know the result.

The second function is very clear in how the condition is evaluated and therefore easier to read than the first.

It is also part of the ZEN of Python (https://www.python.org/dev/peps/pep-0020/): "In the face of ambiguity, refuse the temptation to guess."

onno
  • 969
  • 5
  • 9
0

Parenthesis execute their code first, but that isn't something that you want here. Not is first, but isn't applicable here. And is second and or is last. Parenthesis don't work so as expected. But it is not wrong.

Chris Fowl
  • 488
  • 4
  • 16
  • My problem is this : 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. – s0meth1ngg Oct 26 '18 at 11:00
  • And when i type the first one it doesnt work the same as the 2nd one i dont know why – s0meth1ngg Oct 26 '18 at 11:00
  • Please read this tutorial: https://www.tutorialspoint.com/python/logical_operators_example.htm – Chris Fowl Oct 26 '18 at 11:02
0

Operator precedence to your rescue. Look there for more details.
In your case, the first one translates to "(if this and this) or that" because and has higher precedence. And the second one is self explanatory.

jar
  • 2,646
  • 1
  • 22
  • 47
0

Although you are not specifying what correct means, the difference between the 2 implementations is that the and operator or operators have a different precedence. This means that with out paranthesis the and is evaluated first. See operator precedence in: https://docs.python.org/3/reference/expressions.html

Malcolm X
  • 29
  • 3
0

The precedence order of the and is greater as compare to or.

In the first block of code, the talking == True and hour < 7 expression is evaluated first & its subsequent result will be or with hour > 20 expression.

In the second block of code, the hour < 7 or hour > 20 expression is evaluated first due to inner-round brackets & its subsequent result will be and with talking == True expression.

Usman
  • 1,983
  • 15
  • 28
0

As is the case in most programming languages as well as Boolean algebra, the and operator has a higher precedence than the or operator in Python. So if you want the or operation to be evaluated before the and operation within the same expression, you would need to put parentheses around the or operation.

You can refer to the Operator precedence section of Python's documentation for details.

blhsing
  • 91,368
  • 6
  • 71
  • 106