• [deleted]
    + 15 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:

    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 (n in range (6, 21) or n % 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.

    else:
            return "Weird"
    

    And that's it, really. Best of luck!

    • + 0 comments

      short and good code !

    • + 1 comment

      Works but it's less flexible than a common if-else.

      Moreover you're creating new objects and iterating ;)

      • [deleted]
        + 0 comments

        very true

    • + 1 comment

      hey why the exclamation point? sorry in advance, noob.

      • + 1 comment

        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.

        • + 0 comments

          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.

    • + 1 comment

      showing expected a indented block

      • + 0 comments

        Python relies heavily on indents, it will raise an error if you do not put an indent.

    • + 3 comments

      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

      • + 1 comment

        me too... I even tried running this on my own pycharm compiler and it worked. why is hackerrank compiler so different?

        • + 0 comments

          to me it's showing invalid syntax.

      • + 1 comment

        try n=int(input()) in python 3

        • + 2 comments

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

              what is wrong in this code... I am getting the right output but failing the test case.
          
          • + 0 comments

            because weird spelling is wrong in your code

          • + 1 comment

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

            • + 2 comments

              if n%2 != 0 or 6

              • + 0 comments

                n%2==0 and (n in range(2,6) or n > 20)

              • + 0 comments

                Ain't that way

      • + 0 comments

        write int when you are taking the input n = int(input())

    • + 0 comments

      nice code!:)

    • + 0 comments

      if instead of "or" condition should we put and condition so it can satisfy both

    • + 0 comments

      Hi , In else it is suppose to be "Not Weird".Am i right?

    • + 2 comments

      I am getting a Syntax error 'return' outside function. How can I fix that?

      SyntaxError: 'return' outside function
      
      • + 0 comments

        without function you are returning value so instead of returning you can use print function for result

      • + 0 comments

        REMOVE return

    • + 0 comments

      this is good and short one, thank you

    • + 0 comments

      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 .

    • + 0 comments

      will it work bro?

    • + 0 comments

      but how to define n

    • + 0 comments

      for i in range(0,26+1): if (i%2)!=0: print(f'weird:{i}') else: print(f'notweird:{i}') i=i+1