We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
We're comparing user input to a set of conditions we've set out in our program. If the input matches the conditions, the program will print a message. A basic example of an if statement would be:
if(1>=0):return"Spam"
With regard to the question however, we're given multiple conditions and our program has to respond to those conditions as such. There are three outputs that expect a "Weird" output so we'll do a single if statement to address that
if(ninrange(6,21)orn%2!=0):return"Weird"
Firstly, we are saying if n is between the range of 6 and 21 (21 is not counted) print "Weird", we're also saying that if n is not an even number -- print the same result.
Lastly, we can just use an else statement to print off any remaining statements if none of test cases matched the previous conditions.
And in layman's terms, n % 2 == 0 checks whether the number n is evenly divisible by 2 (or in other words, an even number) whereas n % 2 != 0 is the opposite, checking whether n is an odd number.
At least from what I got it was getting an error when they tried it with "29" possibly due to it discarding it straight into your "else" condition due to being above 20. An elif between it solved it.
n = float(input())
if 1 <= n <= 100:
if (n % 2 != 0 or n >= 6) and n <= 20:
print('Weird')
elif n % 2 != 0:
print("Weird")
else:
print('Not Weird')
Python If-Else
You are viewing a single comment's thread. Return to all comments →
To summarise:
We're comparing user input to a set of conditions we've set out in our program. If the input matches the conditions, the program will print a message. A basic example of an if statement would be:
With regard to the question however, we're given multiple conditions and our program has to respond to those conditions as such. There are three outputs that expect a "Weird" output so we'll do a single if statement to address that
Firstly, we are saying if n is between the range of 6 and 21 (21 is not counted) print "Weird", we're also saying that if n is not an even number -- print the same result.
Lastly, we can just use an else statement to print off any remaining statements if none of test cases matched the previous conditions.
And that's it, really. Best of luck!
short and good code !
Works but it's less flexible than a common if-else.
Moreover you're creating new objects and iterating ;)
very true
hey why the exclamation point? sorry in advance, noob.
If you write n % 2 == 0, you check if the remainder equals 0. When you write n % 2 != 0, you check if the remainder does not equal zero.
And in layman's terms, n % 2 == 0 checks whether the number n is evenly divisible by 2 (or in other words, an even number) whereas n % 2 != 0 is the opposite, checking whether n is an odd number.
showing expected a indented block
Python relies heavily on indents, it will raise an error if you do not put an indent.
I tried your code : if (n in range (6, 21) or n % 2 != 0): print("Weird") else: print("Not Weird")
its showing TypeError : not all arguments converted during string formatting
me too... I even tried running this on my own pycharm compiler and it worked. why is hackerrank compiler so different?
to me it's showing invalid syntax.
try n=int(input()) in python 3
n = int(input()) if(n>=1 and n<=100): if ((n % 2 != 0 or n>=6) and n<=20): print('Wierd') else: print('Not Wierd')
because weird spelling is wrong in your code
At least from what I got it was getting an error when they tried it with "29" possibly due to it discarding it straight into your "else" condition due to being above 20. An elif between it solved it. n = float(input()) if 1 <= n <= 100: if (n % 2 != 0 or n >= 6) and n <= 20: print('Weird') elif n % 2 != 0: print("Weird") else: print('Not Weird')
if n%2 != 0 or 6
n%2==0 and (n in range(2,6) or n > 20)
Ain't that way
write int when you are taking the input n = int(input())
nice code!:)
if instead of "or" condition should we put and condition so it can satisfy both
Hi , In else it is suppose to be "Not Weird".Am i right?
I am getting a Syntax error 'return' outside function. How can I fix that?
without function you are returning value so instead of returning you can use print function for result
REMOVE return
this is good and short one, thank you
Wow ! I didn't even know that
in range(start , stop , step)
can be used as a logical operator. Till now , I only used it in loops .will it work bro?
but how to define n
for i in range(0,26+1): if (i%2)!=0: print(f'weird:{i}') else: print(f'notweird:{i}') i=i+1