0
from cs50 import get_string

# Check valid input
while True:

    # Get user input
    scard = get_string("Enter card number: ")

    # Get length of card number
    length = len(scard)

    if length == 13 or length == 15 or length == 16:
        break


def main():

    # Convert string to int array and reverse
    icard = list(map(int, scard[::-1]))

    digits = 0
    check1 = 0
    check2 = 0
    total = 0

    # Get first numbers
    if scard[0] == '4':

        # VISA Check

        # First Sum ----------------------------------- Rather want checksum function
        for i in icard[1::2]:
            i = i * 2
            if i >= 10:
                i = (i % 10) + (i // 10)
            check1 += i

        # Second Sum
        for i in icard[0::2]:
            check2 += i

        total = check1 + check2

        if total % 10 == 0:
            print("VISA")
            exit()

    else:
        digits = int(scard[0]) * 10 + int(scard[1])

    # AMEX Check
    if digits == 34 or digits == 37:
        # checksum() ----------------------- Where i want to call checksum function

        # First Sum
        for i in icard[1::2]:
            i = i * 2
            if i >= 10:
                i = (i % 10) + (i // 10)
            check1 += i

        # Second Sum
        for i in icard[0::2]:
            check2 += i

        total = check1 + check2

        if total % 10 == 0:
            print("AMEX")
            exit()

    # MASTERCARD Check
    if digits > 50 and digits < 56 or digits == 22:
        # checksum() ----------------------- Where i want to call checksum function

        # First Sum ----------------------------------- Rather want checksum function
        for i in icard[1::2]:
            i = i * 2
            if i >= 10:
                i = (i % 10) + (i // 10)
            check1 += i

        # Second Sum
        for i in icard[0::2]:
            check2 += i

        total = check1 + check2

        if total % 10 == 0:
            print("MASTERCARD")
            exit()

    print("INVALID")

# Checksum Logic unbale to create function to use in main function 
def checksum():

    check1 = 0
    check2 = 0

    # First Sum
    for i in icard[1::2]:
        i = i * 2
        if i >= 10:
            i = (i % 10) + (i // 10)
        check1 += i

    # Second Sum
    for i in icard[0::2]:
        check2 += i

    total = check1 + check2
    print(total)

    return total

# Call main function

if __name__ == "__main__":
    main()

I am trying to make a function at the end of the main function to run the checksum logic instead of writing it in every if statement. I am not doing something right. If someone could direct me that would be much appreciated :)

Also these are the errors from check50

:) credit.py exists.
:) identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:) identifies 5105105105105100 as MASTERCARD
:) identifies 4111111111111111 as VISA
:) identifies 4012888888881881 as VISA
:( identifies 1234567890 as INVALID
    did not find "INVALID\n"
:) rejects a non-numeric input of "foo" 
:) rejects a non-numeric input of "" 

Anyway to get around the one error that did not find "INVALID\n"

1

You'd have to pass the card number to your function, as string or list of digits, whatever you prefer.

The failing test is because you are presented with a positive number, and should treat it as a potential credit card number, but as its length is not in [13, 15, 16], you ask again instead of printing INVALID\n. Check for the input representing a non-negative integer instead.

As the checksum is the same algorithm for all types, so I'd check that before length and first digits.

I don't like that your code considers 400000000000006 to be a correct VISA number. It has 15 digits, VISA should be 13 or 16 only.

And you "reject" an input of foofoofoofoofoo by crashing (none of the tests). Not exactly a graceful way. Either use get_int, or check before conversion, or do some try/except.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .