0

For Pset6 Sentimental/Credit, my code keeps showing a result of INVALID\n no matter what is passed in. Debug50 is not working in Python, so I was not able to work through the lines one by one. Any help would be greatly appreciated. Thank you in advance!

Code:

#Returns company and validity of credit card number
from cs50 import get_int

def main():
    #Inputs positive credit card
    credit = 0
    while credit <= 0:
        credit = get_int("Credit card number: ")
    #Checks company of credit card
    companyname = company(credit)
    #Checks validity of credit card
    if (validity(credit) == 1):
        #Prints company
        print(companyname);
    else:
        #Prints invalidity
        print("INVALID")

def validity(x):
    #Creates an list of ints to get every second digit doubled
    multiplied = []
    length = digits(x)
    #Gets second-last digit doubled
    copy = x // 10
    multiplied.append(copy % 10 * 2)
    for i in range(length + 1 // 2):
        #Gets every second digit doubled
        copy /= 100
        multiplied.append(copy % 10 * 2)
    #Adds the list's digits together
    halfsum = 0
    for m in range(length + 1 // 2):
        l = multiplied[m]
        for j in range(digits(multiplied[m])):
            k = l % 10
            l /= 10
            halfsum += k
    summed = halfsum
    #Adds digits that were not multiplied together
    notmult = x % 10
    copy = x
    for n in range (length):
        copy /= 100
        notmult += copy % 10
    summed += notmult
    #Checks if last digit is 0
    if summed % 10 == 0:
        return 1
    else:
        return 0
    return 0

#Returns company of a credit card number
def company(x):
    #Finds length of CC number
    length = digits(x)
    #Checks if CC is AMEX
    if length == 15:
        copy = x
        while copy >= 100:
            copy /= 10
        if copy == 34 or copy == 37:
            final = "AMEX"
        else:
            final = "INVALID"
    #Checks if CC is VISA
    elif length == 13:
        copy = x
        while copy >= 10:
            copy /= 10
        if copy == 4:
            final = "VISA"
        else:
            final = "INVALID"
    #Checks if CC is MasterCard or VISA
    elif length == 16:
        copy = x
        cop = x
        while copy >= 100:
            copy /= 10
        while (cop >= 10):
            cop /= 10
        if 51 <= copy <= 55:
            final = "MASTERCARD";
        elif cop == 4:
            final = "VISA"
        else:
            final = "INVALID"
    #Determines invalid if none other is true
    else:
        final = "INVALID"
    #Returns answer
    return final

#Returns how many digits there are in a number
def digits(x):
    digits = 0
    copy = x
    #Keeps dividing and counting until number is 0
    while copy != 0:
        copy /= 10
        digits += 1
    #Returns 1 if number originally was 0
    if x == 0:
        digits += 1
    #Returns final answer
    return digits

if __name__ == "__main__":
    main()

Also, thank you Blauelf for the solution!

1

Python 3 no longer has the / division operator work differently for integers and floating point numbers. Instead, / now is floating-point division, while the new operator // is the integer division, truncating the result.

So use //= instead of /=.

1
  • Thank you so much Blauelf! Unfortunately I am relatively new to Python, and so forgot that / in C means // in Python. Thank you!
    – AK2150
    Apr 16 '19 at 10:20

You must log in to answer this question.

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