0

Traceback error message?

Task Given an integer, , perform the following conditional actions: If n is odd, print Weird If n is even and in the inclusive range of 2 to 5, print Not Weird If n is even and in the inclusive range of 6 to 20, print Weird If n is even and greater than 20, print Not Weird N = int(input()) if N % 2 != 0: print("Weird") if N (range(2,5) % 2 == 0): print("Not Weird") Question: It will print the first "weird" okay. But then why do I keep getting an "Traceback (most recent call last): File "..\Playground\", line 4, in <module> if N (range[2,5,1] % 2 == 0): TypeError: 'type' object is not subscriptable" message? I've tried indenting differently, etc. but to no avail. Any ideas? Thanks for any help.

3rd May 2019, 12:59 PM
tristach605
tristach605 - avatar
6 odpowiedzi
+ 3
you are welcome!
4th May 2019, 9:53 AM
Lothar
Lothar - avatar
+ 2
try this - but its not finally tested: N = int(input()) if N % 2 != 0: print("Weird") if N % 2 == 0: if N in range(2,6): print("Not Weird") elif N in range(6,21): print("Weird") elif N > 20: print("Not Weird")
3rd May 2019, 5:56 PM
Lothar
Lothar - avatar
+ 1
Modulus operator work only with two numbers not with list of numbers. You can change this line if N(range(2,5)%2==0): into like this. for i in range(2,6,1): if i%2==0: print("Not Weird")
3rd May 2019, 4:50 PM
Maninder $ingh
Maninder $ingh - avatar
+ 1
Thanks Lothar (of the Hill People?) , Maninder and Maneren!
4th May 2019, 9:02 AM
tristach605
tristach605 - avatar
+ 1
BTW, ended up having to make one small change and extend the range to (6,21) or else it would print 20 as "no output". So "range" discounts the last number (I think I remember reading that somewhere). Anyway, thanks a bunch (Y).. N = int(input()) if N % 2 != 0: print("Weird") if N % 2 == 0: if N in range(2,6): print("Not Weird") elif N in range(6,21): print("Weird") elif N > 20: print("Not Weird")
4th May 2019, 9:58 AM
tristach605
tristach605 - avatar
0
Second condition should be: N % 2 != 0 and N in range(2, 5).
3rd May 2019, 1:05 PM
Maneren
Maneren - avatar