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

Hi! I have a some bugs with my code and I don't know how to debug them. The question and the code is mentioned below. Really appreciate your favor

**Language- Python 3 **

Given an integer,n, perform the following conditional actions:

  • If n is odd, print Weird
  • If n is even and in the inclusive range of 2 to 10, print Not Weird
  • If n is even and in the inclusive range of 11 to 20, print Weird
  • If n is even and greater than 20, print Not Weird

Input Format
A single line containing a positive integer, n.

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

Output Format
Print Weird if the number is weird. Otherwise, print Not Weird.

My Code:-

n=int(input())
if n%2==1:
    print("Weird")
elif n%2==0:
    if n>1:
        if n<11:
            print("Not Weird")
elif n%2==0:
    if n>10:
        if n<21:
            print("Weird")
elif n>20:
    print("Not Weird")

>Solution :

Your first elif will prevent the second one from ever being reached, and the position of the final elif is confusing.

Instead you can merge the other tests, and also combine the if tests into single pairs to make it easier to follow:

n=int(input())
if n%2==1:
    print("Weird")
else:
    if 1 < n < 11:
        print("Not Weird")
    if 10 < n < 21:
        print("Weird")
    if n > 20:
        print("Not Weird")

You can then reduce it further still:

    if 10 < n < 21:
        print("Weird")
    else:
        print("Not Weird")
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