+ 1

what is wrong with this code ? is it because I wrote two ifs in the beginning?

n = int(input().strip()) if n % 2 == 1: print("Weird") if n % 2 == 0 and n >= 2 and n <= 5: print("Not Weird") elif n >= 6 and n <= 20: print("Weird") elif n >= 20: print("Not Weird")

22nd Apr 2022, 3:33 PM
aly nagaty
7 Respostas
+ 2
In if-else blocks : either if or else part gets executed. But in like if... if... if.. all if are checked, each one is independent to other. Your first if only prints for odd numbers, and prints Weird on true. 2nd if checks for even number first and must be in between 2 to 5 , on true prints "Not weird" Else if checks next is number is between 6 to 20, on true prints "Weird" Else prints "Not weird". Your input satisfied on first if so prints "Weird" But by next if, =>last elif so also prints "Not Weird"
22nd Apr 2022, 4:43 PM
Jayakrishna 🇮🇳
+ 1
what is the motive behind this code?? the code works fine. It's fine too write this way, it is totally dependent on your logic
22nd Apr 2022, 3:38 PM
NonStop CODING
NonStop CODING - avatar
0
A message appears to me that input 29 is expected to result as "weird I suppose it should result "weird" in what I wrote but I'm not sure Is the second if code "if n% 2 == 0 is applied to all the if / elif statement afterwards?
22nd Apr 2022, 3:47 PM
aly nagaty
0
aly nagaty 29 will result in "weird" if the 2nd "if" statement is false then control will goes to elif
22nd Apr 2022, 3:50 PM
NonStop CODING
NonStop CODING - avatar
0
aly nagaty but what is your aim of writing code ? are you tryimg to check numbers that are divisible by 2 or not
22nd Apr 2022, 3:51 PM
NonStop CODING
NonStop CODING - avatar
0
aly nagaty looks like you are checking for n%2==0 for all the others elif to. No bro, n%2==0 is onlyy applied to the 2nd if statement
22nd Apr 2022, 3:53 PM
NonStop CODING
NonStop CODING - avatar
0
Then how to make n%2==0 apply for all statements?
22nd Apr 2022, 4:41 PM
aly nagaty