Credit card validator in Python 💳

Поділитися
Вставка
  • Опубліковано 1 січ 2025

КОМЕНТАРІ • 83

  • @BroCodez
    @BroCodez  2 роки тому +41

    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")

  • @krishwadhwani47
    @krishwadhwani47 2 роки тому +44

    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.

  • @ugyenofficial
    @ugyenofficial 2 роки тому +5

    Thank you so much for the lesson bro. I appreciate you for sharing your knowledge

  • @kapibara2440
    @kapibara2440 Рік тому +1

    What a great content! 😊

  • @skyisthelimpet
    @skyisthelimpet 2 роки тому +1

    Very good video as always. Thanks bro code.

  • @MrXbox78
    @MrXbox78 2 роки тому +5

    Great video, perfect for practicing. Keep up the work!

  • @ruthvikas
    @ruthvikas 2 роки тому +1

    Really good tutorial. Thank you!

  • @mrpadz2760
    @mrpadz2760 2 роки тому +1

    Thank you for teaching

  • @yusra-wk6gj
    @yusra-wk6gj 6 місяців тому +2

    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 .

    • @Adiii-wh9rm
      @Adiii-wh9rm 6 місяців тому +2

      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

  • @thecarzyguyyy9692
    @thecarzyguyyy9692 9 місяців тому +1

    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 %. :(((

  • @ishan777x
    @ishan777x Рік тому +5

    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)

  • @jackyisking
    @jackyisking Рік тому +2

    The reverse would be an interesting project as well. Credit card number generator

    • @MasterYI_POV
      @MasterYI_POV Рік тому +2

      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)

  • @TorabekRaufov-p8x
    @TorabekRaufov-p8x 3 місяці тому

    bro is the best

  • @mohammedabdualkrim6192
    @mohammedabdualkrim6192 2 роки тому

    I lave it🙂

  • @ousmanetall1286
    @ousmanetall1286 Рік тому +1

    Programming is not easy that way, but one can be expert by habit, as they said: habit in the genius, but no one!

  • @joelshaji9931
    @joelshaji9931 2 роки тому

    Thank you so much man!

  • @nikilnikki18
    @nikilnikki18 2 роки тому +10

    Bro can you teach us mini projects in python. Which will help engineering students for their resume 🥺

  • @rockichannel3102
    @rockichannel3102 2 роки тому

    very helpfull

  • @alisadcoable
    @alisadcoable 2 роки тому

    very good, thanks bro

  • @Cet.w1p
    @Cet.w1p 2 роки тому

    FIRST thank you bro for learning us:)❤️

  • @midsxma7864
    @midsxma7864 2 роки тому

    Bro you are the boss

  • @ReinoLogistics
    @ReinoLogistics 2 місяці тому

    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?

  • @improvizr7380
    @improvizr7380 2 роки тому

    Bro thank you sooo much ur elite

  • @halite4116
    @halite4116 2 роки тому

    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?

    • @geowhat
      @geowhat 2 роки тому

      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

  • @tara2337
    @tara2337 10 місяців тому

    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🥳

  • @aadijha3347
    @aadijha3347 2 роки тому

    Nice :D

  • @aishnashukla469
    @aishnashukla469 2 роки тому +3

    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

    • @paragwho
      @paragwho Рік тому

      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.

  • @unique_astron_info
    @unique_astron_info Рік тому

    Thanks

  • @عبدالرحمنعاطفعبدالقادرمحمد

    May you make a date structure and algorithms course in c or c++ Bro, please.

  • @hosseinramezani5025
    @hosseinramezani5025 11 місяців тому

    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

  • @mastercheif3029
    @mastercheif3029 10 місяців тому

    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 😭

  • @denislavdochev3783
    @denislavdochev3783 2 роки тому +1

    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?

    • @sasidharnaidu4507
      @sasidharnaidu4507 Рік тому

      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

  • @wtfdoiputhere
    @wtfdoiputhere 2 роки тому

    This could've been way more fun using regex
    I even wrote a blog about that when i finally learned some regex

  • @TheEmjay19
    @TheEmjay19 Рік тому

    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?

  • @autoauto2000
    @autoauto2000 11 місяців тому

    i have only ti84 python. is there a solution with lists?

  • @kunalkamthe8039
    @kunalkamthe8039 2 роки тому

    Bro please make tutorial on React Js

  • @jakesmith1542
    @jakesmith1542 Рік тому

    does it say valid to all cards online or just actual working cards

  • @eduardofabris8523
    @eduardofabris8523 2 роки тому +1

    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

    • @wtfdoiputhere
      @wtfdoiputhere 2 роки тому +1

      Ik I'm not bro but please look into pyautogui if you're a python noob it's crazy awesome

  • @Gabei
    @Gabei 2 роки тому +1

    Can i perform fraudulent activities with this method?

  • @-A-h-m-e-d-
    @-A-h-m-e-d- 2 роки тому

    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.

  • @anon78747
    @anon78747 2 роки тому +5

    Based on the concept, could we make a credit card generator?

  • @ToughNevyHacSoc
    @ToughNevyHacSoc 2 роки тому +1

    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.

  • @ayeItsdevo
    @ayeItsdevo Рік тому

    Luhns algorithm?

  • @dragomirpetrov5138
    @dragomirpetrov5138 2 роки тому

    How do you come up with these ideas :D

  • @picsru4818
    @picsru4818 2 роки тому

    Could you replicate this program in C ? I'm learning C at the moment and I'm curious how it would look

  • @tohereknowswhen779
    @tohereknowswhen779 Рік тому

    i love you

  • @elronhalf-elven6491
    @elronhalf-elven6491 2 роки тому +1

    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.

    • @krishwadhwani47
      @krishwadhwani47 2 роки тому

      To avoid frauds?

    • @niborj4172
      @niborj4172 Рік тому

      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

  • @joymackey8218
    @joymackey8218 2 роки тому

    Where can we download the script ? Is it on github ??

    • @bashar8765
      @bashar8765 2 роки тому

      It is in the description

  • @MoinMoin-ep8jl
    @MoinMoin-ep8jl 2 роки тому

    1.🎉

  • @solivagant_x7
    @solivagant_x7 7 місяців тому

    I understood nothing...

  • @allofmystuffs
    @allofmystuffs 2 роки тому

    So my credit card is INVALID.

  • @BioloAccount
    @BioloAccount Рік тому

    Brooooooooooo

  • @nfsheat1456
    @nfsheat1456 10 місяців тому

    all this time ur credit card number was invalid lol

  • @jayrajsinhjadeja3475
    @jayrajsinhjadeja3475 2 роки тому

    Bro stop! You are destroying it!

  • @mateonoduerme
    @mateonoduerme 2 роки тому +1

    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?

    • @niborj4172
      @niborj4172 Рік тому +8

      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.)

    • @lmonarkl1158
      @lmonarkl1158 Рік тому

      @@niborj4172 Thank you! You helped me to understand it better.

  • @Tarog_
    @Tarog_ 2 роки тому +2

    you lost me bro

    • @user-db2uj9vc7s
      @user-db2uj9vc7s 2 роки тому

      Why is he cancelled?

    • @Tarog_
      @Tarog_ 2 роки тому

      @@user-db2uj9vc7s lmfao no, i just can't wrap my head around the equation

  • @intenzgamerz4882
    @intenzgamerz4882 2 роки тому

    dude i need your help fr do u have any discord or social media account