0

I'm currently re-implementing every one of my projects that could be bettered before I submit them all. Right now, I'm stuck on my re-implementation of pset6 credit. For some reason, it always outputs the first INVALID in my code, no matter what is passed in. I have no idea why this is. I've passed through my code many times and it all makes perfect sense. I would greatly appreciate help in finding the error in my code (maybe it has to do with the return value of cs50.get_float?). Here's my code:

import cs50

def main():    
    # Getting cc number
    print("Number: ", end = "")
    cc = cs50.get_float()
    cc = str(cc)
    # Creating array to hold cc number
    cc_number = []
    # Adding each digit in cc to array
    for i in range(len(cc)):
        cc_number.append(cc[i])
    # If cc is potentially VISA, initialize array according to number of digits
    if len(cc_number) == 13:
        mult2 = []
        mult2.append(cc_number[11], cc_number[9], cc_number[7], cc_number[5], cc_number[3], cc_number[1])
        rest = []
        rest.append(cc_number[12], cc_number[10], cc_number[8], cc_number[6], cc_number[4], cc_number[2], cc_number[0])
    # If cc is potentially AMEX, initialize array according to number of digits 
    elif len(cc_number) == 15:
        mult2 = []
        mult2.append(cc_number[13], cc_number[11], cc_number[9], cc_number[7], cc_number[5], cc_number[3], cc_number[1])
        rest = []
        rest.append(cc_number[14], cc_number[12], cc_number[10], cc_number[8], cc_number[6], cc_number[4], cc_number[2], cc_number[0])
    # If cc is potentially VISA or MASTERCARD, initialize array according to number of digits
    elif len(cc_number) == 16:
        mult2 = []
        mult2.append(cc_number[14], cc_number[12], cc_number[10], cc_number[8], cc_number[6], cc_number[1])
        rest = []
        rest.append(cc_number[12], cc_number[10], cc_number[8], cc_number[6], cc_number[4], cc_number[2], cc_number[0])

    else: # ALWAYS ENTERS THIS CONDITION
        print("INVALID") 
        return 0;

    for letter in len(mult2):
        mult2[letter] = (2 * int(mult2[letter]))
        if mult2[letter] > 9:
            mult2[letter] = 1 + (mult2[letter] % 10)
    cs1 = sum(mult2)

    checksum = cs1 + sum(int(rest))

    cc = int(cc)
    if checksum > 9 and checksum < 141 and checksum % 10 == 0:
        if (cc > 339999999999999 and cc < 349999999999999) or (cc > 369999999999999 and cc < 379999999999999):
            print("AMEX")
        elif (cc > 5099999999999999 and cc < 5599999999999999):
            print("MASTERCARD")
        elif (cc > 3999999999999 and cc < 4999999999999) or (cc > 3999999999999999 and cc < 4999999999999999):
            print("VISA")
    else:
        print("INVALID")
if __name__ == "__main__":
    main()
1

Check this simple program, and see what happens when you get a float from the user using get_float() and then cast it to str. See what is wrong with it and how your program fails?

import cs50


def main():
    x = cs50.get_float("Number: ")
    print(str(x))

if __name__ == "__main__":
    main()

Output:

Number: 20
20.0

Also, your program is full of "magic numbers" like the following lines:

mult2.append(cc_number[13], cc_number[11], cc_number[9], cc_number[7], cc_number[5], cc_number[3], cc_number[1])

rest.append(cc_number[12], cc_number[10], cc_number[8], cc_number[6], cc_number[4], cc_number[2], cc_number[0])

if (cc > 339999999999999 and cc < 349999999999999) or (cc > 369999999999999 and cc < 379999999999999):
    print("AMEX")
elif (cc > 5099999999999999 and cc < 5599999999999999):
    print("MASTERCARD")
elif (cc > 3999999999999 and cc < 4999999999999) or (cc > 3999999999999999 and cc < 4999999999999999):
    print("VISA")

Are you sure that the best way to get input from the user is through a float? Especial when you later immediately cast it to a str?

Also, could you maybe find a way to programmatically add the digits from end to start, without specifying the exact indexes by hand?

Try to add some print()s to your program to see the input you get from the user, what is the result when you cast to str, and what is the length of the str object.

You must log in to answer this question.

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