vovabikini.blogg.se

Vb credit card validator
Vb credit card validator










  1. #VB CREDIT CARD VALIDATOR HOW TO#
  2. #VB CREDIT CARD VALIDATOR GENERATOR#
  3. #VB CREDIT CARD VALIDATOR CODE#

def validate_credit_card_number(card_number):

vb credit card validator

It's a bit like saying that 9 hours ago, the clock hand pointed at the same number as it will do \$(12-9)=3\$ hours from now - except that clocks work modulo 12. Why? Because \$-9 \pmod\$ since the extra 10 doesn't change the modulus. Let's keep going! If we add -9 to a sum that we are reducing modulo 10, that's the same as adding 1 to the sum.

#VB CREDIT CARD VALIDATOR CODE#

This code actually works! Simplifying with modulus tricks But for those numbers, the difference between what we do now and what we should do is always 9! How about adding a check for whether the digit is 5 or more, in which case subtract 9? def validate_credit_card_number(card_number): If the digit at the even index is 0, 1, 2, 3, or 4, we actually do the right thing: double the number. But, alas, there is still an issue with the double digits. def validate_credit_card_number(card_number): #incomplete code All we are doing is adding up numbers, so it wouldn't make any difference if we were to just reverse the list first! We just have to be careful that the rightmost digit in the reverse list ( temp_list) is now at index 0, so we must now be correcting the odd numbers. So what if that digit happens to have an even index? Our odds and evens would be exactly switched! Here, we would notice that it'd be so much more convenient to just count from the right. the Luhn specification said we needed to start counting from the right-most digit. Here we use Python's a if else b ternary notation to check if the number is the even number.

#VB CREDIT CARD VALIDATOR GENERATOR#

To illustrate, enumerate () returns a generator for. Formally, enumerate gives you a generator that provides (index,element) pairs from a list. The check for whether i is odd is just (i % 2) = 1.īut what's a good way of getting to know both the index of the digit and the digit itself? Python has a nice built-in iterator called enumerate. One possible answer is to somehow look up the index of the digit we are processing, and seeing if that one is odd or even. Instead of dividing the list up into evens and odds and separating the problems, what if we had some sort of indicator that we are processing an odd or an even digit? Correcting for the even numbers

vb credit card validator

So what are the corrections we need to make? Well, every other digit needs to be added again. Return ( sum(temp_list) + CORRECTIONS ) % 10 = 0 Since both odds and evens adds each digit, we could start off with just adding up all the digits: def validate_credit_card_number(card_number): #incomplete code Then we also need to account for the cases when the doubling results in two-digit numbers, but more on that below. We will, in fact, also be adding up all the digits in evens, except we will also add them up again (doubling). Recall that for our sum, we will be adding up all the numbers in odds. Finding commonality in how the odds even digits are handled As a side-note about style, more descriptive names of these lists might be evens and odds: hints that would really help the person who has to read your code. If we desire to get a "one-liner" Python script, for what it's worth, let's look at the difference between list1 and list2. I should warn that the code is at a stage where optimizations begin to really flirt with readability, and so for shared code that must be understood and maintained, we're home safe.

#VB CREDIT CARD VALIDATOR HOW TO#

Josay's answer is right on the money in terms of how to start optimizing one's Python code.īelow I go further with the optimization that Josay started. Result=validate_credit_card_number(card_number)

vb credit card validator

My code works, but I wanted to learn about a more efficient and more Pythonic way of doing this. I've implemented Luhn's algorithm for checking credit card numbers.












Vb credit card validator