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

Part of my code is supposed to check if the number entered by user i between 1 to 8 but it's accepting numbers > than 8

# this code is written in python
While True:
    try:
        n  = int(input("height: "))
        if (n > 0 or n < 9):
            break
     except ValueError:
         Print("that's not an integer")
return n 

>Solution :

Looks like it’s a function that keep asking for a number until it’s given a number bigger than 1 OR lower than 9. Not only you put parenthesis on the if, wich you don’t need, but your logic will also fail.
Let’s say you type 100. 100 is bigger than 0. So it will pass. If you want a number between 1 to 8, you should use AND.

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

Try this one:

def check_number():
    while True:
        try:
            n  = int(input("height: "))
            if n > 0 and n < 9:
                break
        except ValueError:
             print("that's not an integer")
    return n 
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