def is_valid_credit_card(card_number):
card_number = card_number.replace(" ", "") # Remove spaces if any
if not card_number.isdigit() or len(card_number) != 16:
return False # Credit card number should be 16 digits
digits = [int(digit) for digit in card_number]
for i in range(14, -1, -2):
digits *= 2
if digits > 9:
digits -= 9
return sum(digits) % 10 == 0
# Example: Check if a credit card number is valid
credit_card_number = "1234 5678 9012 3456"
if is_valid_credit_card(credit_card_number):
print("Valid credit card number.")
else:
print("Invalid credit card number.")
card_number = card_number.replace(" ", "") # Remove spaces if any
if not card_number.isdigit() or len(card_number) != 16:
return False # Credit card number should be 16 digits
digits = [int(digit) for digit in card_number]
for i in range(14, -1, -2):
digits *= 2
if digits > 9:
digits -= 9
return sum(digits) % 10 == 0
# Example: Check if a credit card number is valid
credit_card_number = "1234 5678 9012 3456"
if is_valid_credit_card(credit_card_number):
print("Valid credit card number.")
else:
print("Invalid credit card number.")