Python program that validate the credit card is valid or not

Currently reading:
 Python program that validate the credit card is valid or not

yohilax951

Member
LV
1
Joined
Dec 20, 2023
Threads
63
Likes
32
Awards
5
Credits
3,810©
Cash
0$
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.")
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Tips

Similar threads

Top Bottom