Lowest price ever! Learn Generative AI for 48% less!
Get my discount0
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.
6 Answers
+ 3
you are welcome!
+ 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")
+ 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")
+ 1
Thanks Lothar (of the Hill People?) , Maninder and Maneren!
+ 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")
0
Second condition should be: N % 2 != 0 and N in range(2, 5).