Skip to content Skip to sidebar Skip to footer

Python: List Index Out Of Range Error In Code

I'm pretty new to python, and really stuck at this. Basically, I'm supposed to make a check code to check the last alphabet of the NRIC. My code works fine so long as there are 7 n

Solution 1:

When you convert the string input into an int, the leading zero is stripped away (e.g. "0153444" -> 153444). When you convert back to a string again in the list comprehension, you won't get the zero back, so you end up with an NRIC list of [1, 5, 3, 4, 4, 4] instead of [0, 1, 5, 3, 4, 4, 4]. If you remove the int call, like this, you won't lose the leading zero.

# Change this:
nricno = int(input("Please enter your NRIC(numbers only)..."))
# To this:
nricno = input("Please enter your NRIC(numbers only)...")

Solution 2:

Here's a compact way to calculate the NRIC check code. If an invalid string is passed to the function a ValueError exception is raised, which will cause the program to crash. And if a non-string is passed TypeError will be raised. You can catch exceptions using the try:... except syntax.

def check_code(nric):
    if len(nric) != 7 or not nric.isdigit():
        raise ValueError("Bad NRIC: {!r}".format(nric))

    weights = (2, 7, 6, 5, 4, 3, 2)
    n = sum(int(c) * w for c, w in zip(nric, weights))
    return "ABCDEFGHIZJ"[10 - n % 11]

# Test
nric = "9300007"
print(check_code(nric))

output

B

Solution 3:

Edit: This code verifies if the input is made of 7 digits.

def check_code():       
    while True:

        nricno = input("Please enter your NRIC(numbers only). If you don't type an nric number, this code will restart.")

        if len(nricno) == 7 and nricno.digits == True:
            print ("Works")
            continue
        else:
            print("Error, 7 digit number was not inputted and/or letters and other characters were inputted.")


    a = NRIC[0]*2
    b = NRIC[1]*7
    c = NRIC[2]*6
    d = NRIC[3]*5
    e = NRIC[4]*4
    f = NRIC[5]*3
    g = NRIC[6]*2

    SUM = int(a + b + c + d + e + f +g)

    remainder = int(SUM % 11)

    print(remainder)

    leftovers = int(11 - remainder)

    rightovers = leftovers - 1 

    Alphabet = "ABCDEFGHIZJ"

    checkcode = chr(ord('a') + rightovers)

    print(checkcode.upper())

check_code()

Solution 4:

When you force your input as an int, the leading 0 will be interpreted as an error in Python 3. For example, int(0351) would not yield either 0351 or 351 but would just cause an error stating invalid token.

You should not force the input to be an int, but instead add an assert statement stating that the value inputted must be a 7 digit integer (or a regular statement as you've done if you prefer).

Change this:

nricno = int(input("Please enter your NRIC(numbers only). If you don't type an nric number, this code will fail."))

To this:

nricno = input("Please enter your NRIC(numbers only). If you don't type an nric number, this code will fail.")


Post a Comment for "Python: List Index Out Of Range Error In Code"