Python – How to give a Description to a number

Question:

Could you guys help me in determining how to do this:

Write a function that determines some characteristics of a certain integer n whose value is taken
from a user input. These characteristics include:
• If n is odd or even
• If n is positive, negative, or neither
• If n is divisible by 3
• If the absolute value of n is between 50 and 99 inclusive
After determining its characteristics, your output should be like this:

Enter a number: 69

=== A SHORT DESCRIPTION OF 69 ===

69 is odd

69 is positive

69 is divisible by 3

The absolute value of 69 is between 50 and 99 inclusive.


Here is my code:

n = int(input("ENTER A NUMBER: "))
remainder = n % 2
print("===","SHORT DESCRIPTION OF", n,"===")

if (remainder == 0):
    print(n, "is an EVEN NUMBER")
elif (n % 2) == 0:
    print(n, "is an EVEN NUMBER")
else:
    print(n, "is an ODD NUMBER")

if n > 0:
    print(n, "is a POSITIVE NUMBER")
elif n < 0:
    print(n, "is a NEGATIVE NUMBER")
else:
    print(n, "is NEITHER a POSTIVE or a NEGATIVE NUMBER")

if (n % 3) == 0:
    print(n, "is DIVISIBLE by 3")
else:
    print(n, "is not DIVISIBLE by 3")

if 99 >= n >= 50:
    print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif n < 0:
    99 >= n >= 50
    print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif 99 <= n >= 50:
    print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")

It is almost correct but the issue I am facing is the last part of the code on how do I have the "the absolute value of ‘n’ is/not between 50 and 99 inclusive".

Asked By: Jv1477

||

Answers:

you can use the ‘abs’ function:

  if 99 >= abs(n) >= 50:
        print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
    elif n < 0:
        99 >= abs(n) >= 50
        print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
    elif 99 <= abs(n) >= 50:
        print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")
Answered By: Ran A

You don’t need to test if n < 0 then, you can test if either n, or -n is between 50 and 99, in all other cases the absolute value is not between these two.

here is a possible solution:

if 99 >= n >= 50 or 99 >= -n >= 50:
    print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
else:
    print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")

You also retest the opposite condition when that is actually not necessary. If the first condition is false, it means it’s not between 50 and 99, no need to test again.

As a bonus advice, here are some slight improvements to the rest of your code:

n = int(input("ENTER A NUMBER: "))
print("===","SHORT DESCRIPTION OF", n,"===")

#You were testing this condition twice!
if (n % 2) == 0:
    print(n, "is an EVEN NUMBER")
else:
    print(n, "is an ODD NUMBER")

if n > 0:
    print(n, "is a POSITIVE NUMBER")
elif n < 0:
    print(n, "is a NEGATIVE NUMBER")
else:
    print(n, "is NEITHER a POSTIVE or a NEGATIVE NUMBER")

if (n % 3) == 0:
    print(n, "is DIVISIBLE by 3")
else:
    print(n, "is not DIVISIBLE by 3")

# you can test both condition at once so you don't have to copypaste the print statement
if 99 >= n >= 50 or 99 >= -n >= 50:
    print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
else:
    print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")
Answered By: jeekiii

As suggested in the answer by @Ran A you can use abs(); and once again, there’s no need for two tests – just get the absolute value and check it.

But the real issue is that line elif 99 <= n >= 50:: you are checking if n is greater than 99 and is also greater than 50. This is not what you want. To be honest once you work with the absolute value this test becomes redundant, but let’s keep it. So the last part of your code could be something like:

n = abs(n)
if 99 >= n >= 50:
    print("The ABSOLUTE VALUE of",n,"is between 50 and 99 INCLUSIVE")
elif 99 > n or n < 50: #this could simply be else:
    print("The ABSOLUTE VALUE of",n,"is not between 50 and 99 INCLUSIVE")
Answered By: gimix

Here’s the function you want:

def num_info(n: int) -> None:
    print(f"=== SHORT DESCRIPTION OF {n} ===")

    # even or odd
    if n % 2:
        print(f"{n} is an ODD NUMBER")
    else:
        print(f"{n} is an EVEN NUMBER")

    # positive or negative
    if n > 0:
        print(f"{n} is a POSITIVE NUMBER")
    elif n < 0:
        print(f"{n} is a NEGATIVE NUMBER")
    else:
        print(f"{n} is NEITHER a POSITIVE nor a NEGATIVE NUMBER")

    # divisible by 3
    if n % 3:
        print(f"{n} is not DIVISIBLE by 3")
    else:
        print(f"{n} is DIVISIBLE by 3")

    # absolute value of n is between 50 and 99 (inclusive)
    if 50 <= abs(n) <= 99:
        print(f"The ABSOLUTE VALUE of {n} is between 50 and 99 INCLUSIVE")
    else:
        print(f"The ABSOLUTE VALUE of {n} is not between 50 and 99 INCLUSIVE")

You can test it like this:

def main() -> None:
    n = int(input("ENTER A NUMBER: "))
    num_info(n)


if __name__ == "__main__":
    main()

It’s worth noting that you could break the four parts of your function into their own separate functions to improve readability, but in such a simple example it’s not really necessary.

Answered By: chubercik
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.