-5

I'm trying to create an if-else function which will print 'Weird' if an integer n is odd, 'Not Weird' if it's even and between 2 and 5 inclusive, 'Weird' if it's lies between 6 and 20 and not weird if it's an even integer more than 20. I tried the following code but it wouldn't run, can you tell me what the issue is?

#!/bin/python

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    n = int(input().strip())

if n % 2 = 1:
    print ("Weird")

else:
    if n range(2,5):
        print:("Not Weird")

    else:
        if n range(6,20):
            print("Weird")

        else:
            print("Not Weird")
Yoshikage Kira
  • 1,070
  • 1
  • 12
  • 20
  • 1
    `==` is for equality, not `=`. This is pretty basic syntax issue. Get a proper IDE and it will highlight these issues. – Yoshikage Kira Jun 30 '21 at 19:41
  • 1
    `if n range(2,5):` makes no sense. I assume you meant to do `if n in range(2,5):` [Determine Whether Integer Is Between Two Other Integers?](https://stackoverflow.com/questions/13628791/determine-whether-integer-is-between-two-other-integers) – Yoshikage Kira Jun 30 '21 at 19:43
  • 1
    _I tried the following code but it wouldn't run, can you tell me what the issue is?_ Always post the full traceback you get. It helps us to help you and is actually telling what the problem is. As mentioned by @Goion you want to use `in` - membership operator. – buran Jun 30 '21 at 19:47
  • 1
    you somewhere copy/pasted `if __name__ == '__main__':` but you either did not understand what it is good for or you have serveral IndentationErrors to make it work as it should... – Patrick Artner Jun 30 '21 at 19:57

2 Answers2

0
  1. You haven't defined a function.
  2. You forgot to use the word in to check if something is in a range.
  3. You need to use the == operator to check for equality (not = which is assignment).

You also don't really need ranges here, just simple inequality operators. You also don't need any of the modules you imported.

def is_weird(n: int) -> bool:
    """Calculates whether or not a positive integer is 'weird'."""
    if n % 2 == 1:
        return True
    if n <= 5:
        return False
    if n <= 20:
        return True
    return False

def print_weirdness(n: int) -> None:
    """Prints whether or not a positive integer is 'weird'."""
    print("Weird" if is_weird(n) else "Not Weird")

n = int(input().strip())
print_weirdness(n)
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

I hope this helps! If this is not exactly what you were looking for, you can still use my code but reorganize the parts.

n = int(input('Write your number : ').strip())

if n % 2 == 1:
    #n is odd
    print ("Weird")

else:
    #n is even
    if 2 <= n <= 5:
        #n between 2 and 5 inclusive
        print("Not Weird")

    elif 6 <= n <= 20:
        #n between 6 and 20 inclusive
        print("Weird")

    elif n > 20:
        #n greater than 20
        print("Not Weird")
Nicolas
  • 16
  • 3