Test Credit Card Account Numbers American Express 378282246310005 American Express 371449635398431 American Express Corporate 378734493671000 Australian Bankcard 5610591081018250 Diners Club 30569309025904 Diners Club 38520000023237 Discover 6011111111111117 Discover 6011000990139424 JCB 3530111333300000 JCB 3566002020360505 MasterCard 5555555555554444 MasterCard 5105105105105100 Visa 4111111111111111 Visa 4012888888881881 # Python credit card validator program # 1. Remove any '-' or ' ' # 2. Add all digits in the odd places from right to left # 3. Double every second digit from right to left. # (If result is a two-digit number, # add the two-digit number together to get a single digit.) # 4. Sum the totals of steps 2 & 3 # 5. If sum is divisible by 10, the credit card # is valid sum_odd_digits = 0 sum_even_digits = 0 total = 0 # Step 1 card_number = input("Enter a credit card #: ") card_number = card_number.replace("-", "") card_number = card_number.replace(" ", "") card_number = card_number[::-1] # Step 2 for x in card_number[::2]: sum_odd_digits += int(x) # Step 3 for x in card_number[1::2]: x = int(x) * 2 if x >= 10: sum_even_digits += (1 + (x % 10)) else: sum_even_digits += x # Step 4 total = sum_odd_digits + sum_even_digits # Step 5 if total % 10 == 0: print("VALID") else: print("INVALID")
American Express credit cards only start from 34 or 37 and are 15 numbers long. Mastercard only starts from 51, 52, 53, 54 or 55 and is 16 numbers long. Visa cards only start from 4 and they can be 13 or 16 numbers long. You can add that into your code and it will classify credit card numbers as Visa, Mastercard or American Express.
I can tell u work really hard on it to make it easy and understandable but I don't know why I'm still struggling with it I really want to learn this so I can study robotics since I cant afford a teacher right now but I'm starting to think I cant do this .
Bro, your lessons have been superb and very understandable. I will be working on a project on how to program a device in detecting bed bugs in homes. Please, can I have any clue?
So x at first is a string because its iterating a string. But remember how he removed the spaces and hyphens (-)? It means that every character in the string is a digit. So he does x = int(x) * 2 which means he casts it to type int. It works because as i said all characters in the string are digits
I tried to do it myself first, and it took me over an hour🥺 I just make small mistakes and then can't understand what the problem is. I also work with the Vscode which I think has some bugs and issues that confused me. anyways, you rock Bro🥳
hey, haven't you interchanged the logic of sum_odd_digits and sum_even_digits ? I think in step 2 the slicing you did is for even digits not for odd because it starts from zero
No, The slicing in Step 2 is correct. Index 0 always refers to the 1st character of the string(and first position is odd); after that due to step(the parameter) being equal to 2, the next index will be 2 which refers to the 3rd character(and the 3rd characters is at the 3rd position which is an "odd place"). The next index will be 4 and then 6 etc. The slicing is correct in Step 3 as well, think about it once more.
as a juniur develepor im stucked in the idea of writing the code in shortest way possible! is it good or bad? for example this is what i wrote for this program: credit_card = input("Please enter your credit card: ").replace(" ", "").replace("_", "") even_sum = sum(int(i) for i in tuple(credit_card[-1::-2])) odd_sum = sum(math.floor(int(i) * 2 / 10) + (int(i) * 2) % 10 for i in tuple(credit_card[-2::-2])) print("valid" if not (even_sum + odd_sum) % 10 else "invalid") for some of the functions ive got help from the internet, but its pretty much the same in how it works... but im not sure if beeing readable is more important or being short
Bro, thank you. I was stuck with this problem yesterday. I actually got stuck around the sum of the even digits part (every second number from the left) and also how to split and add them if they are greater or equal to 10 😢. I feel so dumb, I tried to use reverse indexing and then I tried to store the value in a list then sum the list each time 😭
Great video again. In order to create a string with the credit card number wouldn't be easier to run a for loop over the input and if the i.isdigit to concatenate it to an empty string that in the end will contain only digit characters?
I don't get the "for x in card_number[1::2]:" in step 3... does that mean that first number is ommitted? Because number count always starts with 0 right?
Bro, could you please make a video explaining how do I take screenshots of my pc with python and create a script to click with the mouse and do something to me. Thank u
Hey bro, i want to ask if u have any idea about why school banned some very basic functions like replace, or print(array), or break for loops.. i dont really understand why is that.
Hey Bro Code, Can you please please help me with creating a draw method in a tick-tak-toe game you created your-self, I really want that, and I've been trying for week, but I could not figure any way out. Please if you know how to create one, replay in my comment.
the criteria he gives us, the ones we are supposed to check are checked every time a credit card is used online. He is simply showing us how to do these checks to see if a credit card number is valid. If not people could make up any card number. We have to check that the given conditions are met. If credit card numbers could have any enumeration / random numbers, we could make up card numbers randomly
I don't understand a lot of things going on in this tutorial, i just did the same that bro was doing with a test credit card number which resulted valid. I'm mainly having difficulty to understand the 'for x in' logic, and specially where it's replaced with (+=) Anyways I haven't do math exercises since i was at school, so i want to ask if anyone can recommend me where can i study maths/arithmetics for programming?
If you dont understand the "for x in" check a while loop and for loop tutorial. (Not the best explanation but i tried my best) for loops are used to iterate through an iterable object. With the x it just refers to the value we are iterating through. We are naming a variable in the for loop, we are kind of naming a variable that changes value every time we go through the loop. numbers = 1234 for x in numbers: print(x) If run this you will see output is: 1 2 3 4 It just shows you that every time we iterate through the loop the value of x changes until we hit the end of the for loop. the part of (+=) is an abbreviation* just to write less code. We are telling the code to add a number to the chosen variable for example sum_even_digits so we do: example: sum_even_digits = 0 nums = "123456" for x in nums: if int(x) % 2 == 0: sum_even_digits = sum_even_digits + int(x) print(sum_even_digits) #Result should be 12 #(This asks the code to add all even numbers to sum_even_digits) sum_even_digits = sum_even_digits + x to abbreviate this we do: sum_even_digits += x Its written differently but means exactly the same. (Fell free to correct me if im wrong, tried to explain it simply.)
Test Credit Card Account Numbers
American Express 378282246310005
American Express 371449635398431
American Express Corporate 378734493671000
Australian Bankcard 5610591081018250
Diners Club 30569309025904
Diners Club 38520000023237
Discover 6011111111111117
Discover 6011000990139424
JCB 3530111333300000
JCB 3566002020360505
MasterCard 5555555555554444
MasterCard 5105105105105100
Visa 4111111111111111
Visa 4012888888881881
# Python credit card validator program
# 1. Remove any '-' or ' '
# 2. Add all digits in the odd places from right to left
# 3. Double every second digit from right to left.
# (If result is a two-digit number,
# add the two-digit number together to get a single digit.)
# 4. Sum the totals of steps 2 & 3
# 5. If sum is divisible by 10, the credit card # is valid
sum_odd_digits = 0
sum_even_digits = 0
total = 0
# Step 1
card_number = input("Enter a credit card #: ")
card_number = card_number.replace("-", "")
card_number = card_number.replace(" ", "")
card_number = card_number[::-1]
# Step 2
for x in card_number[::2]:
sum_odd_digits += int(x)
# Step 3
for x in card_number[1::2]:
x = int(x) * 2
if x >= 10:
sum_even_digits += (1 + (x % 10))
else:
sum_even_digits += x
# Step 4
total = sum_odd_digits + sum_even_digits
# Step 5
if total % 10 == 0:
print("VALID")
else:
print("INVALID")
hey bro , why don't you make another playlist of python where you will add the new videos
hi
American Express credit cards only start from 34 or 37 and are 15 numbers long. Mastercard only starts from 51, 52, 53, 54 or 55 and is 16 numbers long. Visa cards only start from 4 and they can be 13 or 16 numbers long. You can add that into your code and it will classify credit card numbers as Visa, Mastercard or American Express.
Thank you so much for the lesson bro. I appreciate you for sharing your knowledge
What a great content! 😊
Very good video as always. Thanks bro code.
Great video, perfect for practicing. Keep up the work!
Really good tutorial. Thank you!
Thank you for teaching
Esto para que sirve ?
I can tell u work really hard on it to make it easy and understandable but I don't know why I'm still struggling with it I really want to learn this so I can study robotics since I cant afford a teacher right now but I'm starting to think I cant do this .
dw mate even i couldnt get it in the first try doing it more and more youll be able to do it do not give up
Very nice video! But it's so complicated for me, still don't know when to use loops (esp for loop andd nested loop) and when to use %. :(((
Another way of doing step3 in short would be:
for y in user_input[1::2]:
z = int(y) * 2
for i in str(z):
sum_even_digits = sum_even_digits + int(i)
Nice.
brilliant 🤩
The reverse would be an interesting project as well. Credit card number generator
import random
def generate_credit_card():
# Generate a random 15-digit credit card number
issuer_identifier = random.randint(1, 9)
account_number = random.randint(1e14, 9e14)
checksum = calculate_checksum(issuer_identifier, account_number)
credit_card_number = int(str(issuer_identifier) + str(account_number) + str(checksum))
return credit_card_number
def calculate_checksum(issuer_identifier, account_number):
# Calculate the checksum digit using Luhn algorithm
partial_card_number = str(issuer_identifier) + str(account_number)
digits = [int(x) for x in partial_card_number]
for i in range(len(digits) - 2, -1, -2):
digits[i] *= 2
if digits[i] > 9:
digits[i] -= 9
total = sum(digits)
checksum = (total * 9) % 10
return checksum
# Generate and print 10 credit card numbers for testing
for _ in range(10):
card_number = generate_credit_card()
print(card_number)
bro is the best
I lave it🙂
Programming is not easy that way, but one can be expert by habit, as they said: habit in the genius, but no one!
Thank you so much man!
Bro can you teach us mini projects in python. Which will help engineering students for their resume 🥺
very helpfull
very good, thanks bro
FIRST thank you bro for learning us:)❤️
Bro i was first
@@jayrajsinhjadeja3475 no first i commented
@@Cet.w1p ok brother
Bro you are the boss
Bro, your lessons have been superb and very understandable.
I will be working on a project on how to program a device in detecting bed bugs in homes. Please, can I have any clue?
Bro thank you sooo much ur elite
6:12 Why does it work?x should be a string and give error when comparing to int.I'm bad at it, can somebody explain?
So x at first is a string because its iterating a string. But remember how he removed the spaces and hyphens (-)? It means that every character in the string is a digit. So he does x = int(x) * 2 which means he casts it to type int. It works because as i said all characters in the string are digits
I tried to do it myself first, and it took me over an hour🥺 I just make small mistakes and then can't understand what the problem is. I also work with the Vscode which I think has some bugs and issues that confused me. anyways, you rock Bro🥳
Nice :D
hey, haven't you interchanged the logic of sum_odd_digits and sum_even_digits ? I think in step 2 the slicing you did is for even digits not for odd because it starts from zero
No, The slicing in Step 2 is correct. Index 0 always refers to the 1st character of the string(and first position is odd); after that due to step(the parameter) being equal to 2, the next index will be 2 which refers to the 3rd character(and the 3rd characters is at the 3rd position which is an "odd place"). The next index will be 4 and then 6 etc.
The slicing is correct in Step 3 as well, think about it once more.
Thanks
May you make a date structure and algorithms course in c or c++ Bro, please.
as a juniur develepor im stucked in the idea of writing the code in shortest way possible! is it good or bad? for example this is what i wrote for this program:
credit_card = input("Please enter your credit card: ").replace(" ", "").replace("_", "")
even_sum = sum(int(i) for i in tuple(credit_card[-1::-2]))
odd_sum = sum(math.floor(int(i) * 2 / 10) + (int(i) * 2) % 10 for i in tuple(credit_card[-2::-2]))
print("valid" if not (even_sum + odd_sum) % 10 else "invalid")
for some of the functions ive got help from the internet, but its pretty much the same in how it works... but im not sure if beeing readable is more important or being short
Bro, thank you. I was stuck with this problem yesterday. I actually got stuck around the sum of the even digits part (every second number from the left) and also how to split and add them if they are greater or equal to 10 😢.
I feel so dumb, I tried to use reverse indexing and then I tried to store the value in a list then sum the list each time 😭
Great video again. In order to create a string with the credit card number wouldn't be easier to run a for loop over the input and if the i.isdigit to concatenate it to an empty string that in the end will contain only digit characters?
Or you could just remove all whitespace, symbols and then check if that is a number or not. Python supports large numbers, so no issue
This could've been way more fun using regex
I even wrote a blog about that when i finally learned some regex
I don't get the "for x in card_number[1::2]:" in step 3... does that mean that first number is ommitted? Because number count always starts with 0 right?
i have only ti84 python. is there a solution with lists?
Bro please make tutorial on React Js
does it say valid to all cards online or just actual working cards
Bro, could you please make a video explaining how do I take screenshots of my pc with python and create a script to click with the mouse and do something to me. Thank u
Ik I'm not bro but please look into pyautogui if you're a python noob it's crazy awesome
Can i perform fraudulent activities with this method?
Hey bro, i want to ask if u have any idea about why school banned some very basic functions like replace, or print(array), or break for loops.. i dont really understand why is that.
Based on the concept, could we make a credit card generator?
math.random
Hey Bro Code, Can you please please help me with creating a draw method in a tick-tak-toe game you created your-self, I really want that, and I've been trying for week, but I could not figure any way out. Please if you know how to create one, replay in my comment.
Luhns algorithm?
How do you come up with these ideas :D
Could you replicate this program in C ? I'm learning C at the moment and I'm curious how it would look
i love you
Why are we doing all of this random math to tell us if it is valid or not ? I love math, but I'm confused as to why we are doing all of this.
To avoid frauds?
the criteria he gives us, the ones we are supposed to check are checked every time a credit card is used online. He is simply showing us how to do these checks to see if a credit card number is valid. If not people could make up any card number.
We have to check that the given conditions are met.
If credit card numbers could have any enumeration / random numbers, we could make up card numbers randomly
Where can we download the script ? Is it on github ??
It is in the description
1.🎉
I understood nothing...
So my credit card is INVALID.
Brooooooooooo
all this time ur credit card number was invalid lol
Bro stop! You are destroying it!
I don't understand a lot of things going on in this tutorial, i just did the same that bro was doing with a test credit card number which resulted valid. I'm mainly having difficulty to understand the 'for x in' logic, and specially where it's replaced with (+=)
Anyways I haven't do math exercises since i was at school, so i want to ask if anyone can recommend me where can i study maths/arithmetics for programming?
If you dont understand the "for x in" check a while loop and for loop tutorial.
(Not the best explanation but i tried my best)
for loops are used to iterate through an iterable object. With the x it just refers to the value we are iterating through. We are naming a variable in the for loop, we are kind of naming a variable that changes value every time we go through the loop.
numbers = 1234
for x in numbers:
print(x)
If run this you will see output is:
1
2
3
4
It just shows you that every time we iterate through the loop the value of x changes until we hit the end of the for loop.
the part of (+=) is an abbreviation* just to write less code.
We are telling the code to add a number to the chosen variable for example sum_even_digits so we do:
example:
sum_even_digits = 0
nums = "123456"
for x in nums:
if int(x) % 2 == 0:
sum_even_digits = sum_even_digits + int(x)
print(sum_even_digits) #Result should be 12
#(This asks the code to add all even numbers to sum_even_digits)
sum_even_digits = sum_even_digits + x
to abbreviate this we do:
sum_even_digits += x
Its written differently but means exactly the same.
(Fell free to correct me if im wrong, tried to explain it simply.)
@@niborj4172 Thank you! You helped me to understand it better.
you lost me bro
Why is he cancelled?
@@user-db2uj9vc7s lmfao no, i just can't wrap my head around the equation
dude i need your help fr do u have any discord or social media account