Home > Enterprise >  Part of my code is supposed to check if the number entered by user i between 1 to 8 but it's ac
Part of my code is supposed to check if the number entered by user i between 1 to 8 but it's ac

Time:07-12

# 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 

CodePudding user response:

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.

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 

CodePudding user response:

You should use "and" instead of "or"

  • Related