Tell us what’s happening:
The card number 4111-1111-1111-1111 is a invalid number I test it by following the steps shown in the instructions.
But there is a test that wants this number to be valid
I tried to create a special case but that doesn’t work.
So how do I deal with this apparent paradox? Am I doing something wrong?
(there are some print() functions to follow the code)
Your code so far
def verify_card_number(digits):
#stringify
digits = str(digits)
#clean up
digits = digits.replace("-","")
digits = digits.replace(" ","")
#confirm
if not digits.isdigit():
raise TypeError ("The card number should only contain digits")
#verify
last_digit = digits[-1]
print(f"The last digit is {last_digit}")
remain_digits = digits[:-1]
print(f"The remain digits are {remain_digits}")
digit_sum = int(last_digit)
position = 0
for digit in remain_digits:
print(f"For position {position} the number were {digit}")
if position/2 == position//2:
number = int(digit)
else:
number = str(int(digit)*2)
if len(number)>1:
number= int(number[0]) + int(number[1])
else:
number = int(number)
print(f"The number became {number}")
position +=1
digit_sum += number
print(f"On goin sum:{digit_sum}")
if digit_sum/10 == digit_sum//10:
return "VALID!"
else:
return "INVALID!"
#print(verify_card_number("4 5 3 9 1 4 8 8 1"))
#print(verify_card_number("453914889"))
print(verify_card_number("4111-1111-1111-1111"))
#print(verify_card_number("1234 5678 9012 3456"))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36
Challenge Information:
Implement the Luhn Algorithm - Implement the Luhn Algorithm