-1
n = 24
if n % 2 == 0 (n >= 6 and n <= 20):
    print("Weird")

...emits the error message:

TypeError: 'int' object is not callable

What does this mean? How can it be avoided?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
micmicc
  • 9
  • 1
  • 3
    `0 (n >= 6 and n <= 20)` is definitely wrong, but it's not clear what you are trying to do. Please see [ask] and edit your question accordingly. – kaya3 Feb 11 '20 at 00:18
  • 3
    What is the condition `n % 2 == 0 (n >= 6 and n <= 20)` intended to mean? – khelwood Feb 11 '20 at 00:20
  • 1
    BTW, the edits I've made (showing only the *shortest possible code* that demonstrates the problem; pruning prose that isn't necessary to understanding the issue; and making the title descriptive of the specific question) are things you should try to follow in your own questions going forward. – Charles Duffy Feb 11 '20 at 00:24
  • 4
    variable followed by parenthesis is interpreted as calling a function, so `x(...)` is a function call, so is `0(...)`, except that integers aren't callable. – Tadhg McDonald-Jensen Feb 11 '20 at 00:24
  • Always share the entire error message. – AMC Feb 11 '20 at 01:20
  • Does this answer your question? [TypeError: 'int' object is not callable](https://stackoverflow.com/questions/9767391/typeerror-int-object-is-not-callable) – AMC Feb 11 '20 at 01:20

2 Answers2

2

You need the and or or operator to combine the modulus condition and the range condition.

Python also allows you to use chained comparisons to test if a number is in a range.

if n % 2 == 0 or 6 <= n <= 20:
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

you are equating the division to 0 and the other condition all together.

you have to separate the steps, equal to zero and then check.

n = 24

if n % 2 == 0: print ("Not Weird") else: print ("Weird")

if n % 2 == 0 & (n >= 6 and n <= 20): print("Weird")

if n % 2 == 0 & (n <= 2 and n >= 5): print("Not Weird")

if n % 2 == 0 & (n > 20): print("Not Weird")

Remato
  • 93
  • 9
  • 1
    `and` should be used rather than `&` here, since the OP is trying to do a *logical* AND rather than a *bitwise* one. – Charles Duffy Feb 11 '20 at 00:25
  • 1
    @CharlesDuffy `&` works on booleans, it's just unusual to use it because normally the short-circuiting `and` is better. – kaya3 Feb 11 '20 at 00:27
  • Is there a reason you'd argue that the short-circuiting `and` isn't better in this case? – Charles Duffy Feb 11 '20 at 00:29
  • The error is happening because OP is writing `0 (n >= 6 and n <= 20)` which is interpreted as "call the `0` method and pass it the result of this boolean operation as an argument" when Python looks to see what `0` is, it finds it is not a `callable` object, but an `int` object, and `int` is not callable. – Z4-tier Feb 11 '20 at 00:39
  • @CharlesDuffy I don't think @kaya3 was suggesting that `&` is better here, just that it *does* work. – Z4-tier Feb 11 '20 at 00:41
  • @CharlesDuffy is there any performance difference to `and` / `&`? in the end all logic is done in binary comparation, or not? – Remato Feb 11 '20 at 00:48
  • 1
    Whole point of short-circuiting is that you avoid running the logic on the RHS if the LHS obviates it. Running less code is generally preferable to running more, particularly if they both get you the same outcome. – Charles Duffy Feb 11 '20 at 01:12