Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit904d289

Browse files
committed
add credit card validation tutorial
1 parent0d014e4 commit904d289

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
223223
-[How to Make a Hangman Game in Python](https://www.thepythoncode.com/article/make-a-hangman-game-in-python). ([code](python-standard-library/hangman-game))
224224
-[How to Use the Argparse Module in Python](https://www.thepythoncode.com/article/how-to-use-argparse-in-python). ([code](python-standard-library/argparse))
225225
-[How to Make a Grep Clone in Python](https://thepythoncode.com/article/how-to-make-grep-clone-in-python). ([code](python-standard-library/grep-clone))
226+
-[How to Validate Credit Card Numbers in Python](https://thepythoncode.com/article/credit-card-validation-in-python). ([code](python-standard-library/credit-card-validation))
226227

227228
-###[Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
228229
-[How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Validate Credit Card Numbers in Python](https://thepythoncode.com/article/credit-card-validation-in-python)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
importargparse# Import argparse for command-line argument parsing
2+
importre# Import re for regular expression matching
3+
4+
# Validate credit card number using Luhn Algorithm
5+
defluhn_algorithm(card_number):
6+
defdigits_of(n):
7+
return [int(d)fordinstr(n)]# Convert each character in the number to an integer
8+
9+
digits=digits_of(card_number)# Get all digits of the card number
10+
odd_digits=digits[-1::-2]# Get digits from the right, skipping one digit each time (odd positions)
11+
even_digits=digits[-2::-2]# Get every second digit from the right (even positions)
12+
13+
checksum=sum(odd_digits)# Sum all odd position digits
14+
fordineven_digits:
15+
checksum+=sum(digits_of(d*2))# Double each even position digit and sum the resulting digits
16+
17+
returnchecksum%10==0# Return True if checksum modulo 10 is 0
18+
19+
20+
# Function to check credit card number using Luhn's alogorithm
21+
defcheck_credit_card_number(card_number):
22+
card_number=card_number.replace(' ','')# Remove spaces from the card number
23+
ifnotcard_number.isdigit():# Check if the card number contains only digits
24+
returnFalse
25+
returnluhn_algorithm(card_number)# Validate using the Luhn algorithm
26+
27+
# Function to get the card type based on card number using RegEx
28+
defget_card_type(card_number):
29+
card_number=card_number.replace(' ','')# Remove spaces from the card number
30+
card_types= {
31+
"Visa":r"^4[0-9]{12}(?:[0-9]{3})?$",# Visa: Starts with 4, length 13 or 16
32+
"MasterCard":r"^5[1-5][0-9]{14}$",# MasterCard: Starts with 51-55, length 16
33+
"American Express":r"^3[47][0-9]{13}$",# AmEx: Starts with 34 or 37, length 15
34+
"Discover":r"^6(?:011|5[0-9]{2})[0-9]{12}$",# Discover: Starts with 6011 or 65, length 16
35+
"JCB":r"^(?:2131|1800|35\d{3})\d{11}$",# JCB: Starts with 2131, 1800, or 35, length 15 or 16
36+
"Diners Club":r"^3(?:0[0-5]|[68][0-9])[0-9]{11}$",# Diners Club: Starts with 300-305, 36, or 38, length 14
37+
"Maestro":r"^(5018|5020|5038|56|57|58|6304|6759|676[1-3])\d{8,15}$",# Maestro: Various starting patterns, length 12-19
38+
"Verve":r"^(506[01]|507[89]|6500)\d{12,15}$"# Verve: Starts with 5060, 5061, 5078, 5079, or 6500, length 16-19
39+
}
40+
41+
forcard_type,patternincard_types.items():
42+
ifre.match(pattern,card_number):# Check if card number matches the pattern
43+
returncard_type
44+
return"Unknown"# Return Unknown if no pattern matches
45+
46+
47+
# Processing a file containing card numbers.
48+
defprocess_file(file_path):
49+
50+
try:
51+
withopen(file_path,'r')asfile:# Open the file for reading
52+
card_numbers=file.readlines()# Read all lines from the file
53+
results= {}
54+
forcard_numberincard_numbers:
55+
card_number=card_number.strip()# Remove any leading/trailing whitespace
56+
is_valid=check_credit_card_number(card_number)# Validate card number
57+
card_type=get_card_type(card_number)# Detect card type
58+
results[card_number]= (is_valid,card_type)# Store result
59+
returnresults
60+
exceptExceptionase:
61+
print(f"Error reading file:{e}")# Print error message if file cannot be read
62+
returnNone
63+
64+
65+
defmain():
66+
parser=argparse.ArgumentParser(description="Check if a credit card number is legitimate and identify its type using the Luhn algorithm.")
67+
parser.add_argument('-n','--number',type=str,help="A single credit card number to validate.")# Argument for single card number
68+
parser.add_argument('-f','--file',type=str,help="A file containing multiple credit card numbers to validate.")# Argument for file input
69+
70+
args=parser.parse_args()# Parse command-line arguments
71+
72+
ifargs.number:
73+
is_valid=check_credit_card_number(args.number)# Validate single card number
74+
card_type=get_card_type(args.number)# Detect card type
75+
print(f"[!] Credit card number{args.number} is{'valid'ifis_validelse'invalid'} and is of type{card_type}.")# Print result
76+
77+
ifargs.file:
78+
results=process_file(args.file)# Process file with card numbers
79+
ifresults:
80+
forcard_number, (is_valid,card_type)inresults.items():
81+
print(f"[!] Credit card number{card_number} is{'valid'ifis_validelse'invalid'} and is of type{card_type}.")# Print results for each card number
82+
83+
# Execute tha main function
84+
if__name__=='__main__':
85+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
4111111111111111
2+
5555555555554444
3+
378282246310005

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp