Untitled

Untitled

Untitled

Untitled

Untitled

Untitled

Untitled

Untitled

data = list(range(10))
print(data)

try:
    print(data[len(data)])
except IndexError:
    print(f'Index out of data array bounds {len(data)}')

try:
    print(data[len(data)-2])
except IndexError:
    print(f'Index out of data array bounds {len(data)}')
else:
    print(f'there has no error occure')

try:
    print(data[len(data)])
except IndexError as e:
    print(f'Error: {e}')
    print(f'Index out of data array bounds {len(data)}')

else:
    print(f'there has no error occure')

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Index out of data array bounds 10 8 there has no error occure Error: list index out of range Index out of data array bounds 10

Untitled

Untitled

Untitled

Untitled

pw = ''

def setPw(newPW):
    if len(newPW) < 8:
        raise Exception(f'The length of the new password "{newPW}" is too short')
    if len(newPW) > 20:
        raise Exception(f'The length of the new password "{newPW}" is over 20 characters.')
    else:
        pw = newPW

testPw = input('new password:')
setPw(testPw)

Untitled

class Error(Exception):
    """Base class for other exceptions"""
    pass

class ValueTooSmallError(Error):
    """Raised when the input value is too small"""
    pass

class ValueTooLargeError(Error):
    """Raised when the input value is too large"""
    pass

number = 10
while True:
    try:
        i_num = int(input("Enter a number: "))
        if i_num < number:
            raise ValueTooSmallError
        elif i_num  > number:
            raise ValueTooLargeError
        break
    except ValueTooSmallError:
        print("This value is too small, try again!")
        print()
    except ValueTooLargeError:
        print("This value is too large, try again!")
        print()

print("Congratulation! You guessed it correctly")

Untitled

Untitled