简体   繁体   中英

What does a "TypeError: 'int' object is not callable" mean?

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?

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:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM