Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – How to give a Description to a number

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

=== 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".

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading