TypeError: '<' not supported between instances of 'list' and 'int

Im not able to solve these isssues: File "c:\Data\test_while.py", line 7, in if x < 0 or x > 5: TypeError: '<' not supported between instances of 'list' and 'int'

x = []

while True:
    try:
        x.append(int(input("Please enter a number 1 - 5: ")))
        if x < 0 or x > 5:
                ValueError
                print ("Wrong numbers...")
                continue
        if x == 0:
            break
        print (x)
    except ValueError:
        print ("Oops!  Only numbers. Try again...")

print(x[:-1])

Solution 1:

You probably don't want to add the number to the list when it is outside your bounds. Only append it when it passes checks.

Also, in this bit of code there probably isn't a need to use exceptions, but if you feel the need or curious, you can do so as I have below. I don't know why you have ValueError in your bounds check because it doesn't raise that way.

x = []

while True:
    try:
        cur = int(input("Please enter a number 1 - 5: "))
        if cur < 0 or cur > 5:
            raise ValueError("Wrong numbers ...")
        if cur == 0:
            break
        x.append(cur)
        print(cur)
    except ValueError as e:
        if "invalid literal" in str(e):
            print("Oops!  Only numbers. Try again...")
        elif "Wrong numbers" in str(e):
            print(e)
        else:
            raise

print(x)

It would be nice if the type check for integer conversion would raise a TypeError instead of ValueError when invalid literal happens, but I'm sure there is some good reason that isn't the case.