Question

In: Computer Science

please give complete code in python using def function thank you! Validate Credit Card Numbers Credit...

please give complete code in python using def function thank you!

Validate Credit Card Numbers

Credit card numbers can be quickly validated by the Luhn checksum algorithm.

  1. Start at the rightmost digit save one (the last being the checksum digit). Moving left, double the value of every second digit. If the doubled value is greater than nine, then subtract 9 from the doubled value to renormalize within the range 0–9.
  2. Sum all the digits including the checksum digit.
  3. If this total modulo 10 is zero, the number is valid.

For instance, Ned Flander's credit card number is 8525.4941.2525.4158.

  1. Here are the digits with checksum underlined:

    8525494125254158_

  2. Here are the digits doubled back:

    16 5 4 5 8 9 8 1 4 5 4 5 8 1 10 8_

    Here are the two-digit results subtracted nine:

    7 5 4 5 8 9 8 1 4 5 4 5 8 1 1 8_

  3. Here is the sum:

    7+5+4+5+8+9+8+1+4+5+4+5+8+1+1+8=83

  4. Here is the modulo 10 result: 83%10=3 and this is not a valid credit card number.

Compose a function luhn which correctly validates a given candidate 16-digit credit-card number (as a numeric input in int).

Solutions

Expert Solution

I have written the python code as per your requirements, kindly refer to comment line in code for better clarity.

I have also uploaded the output of the code.

# luhn function to validate whether a credit card number is valid or not
def luhn(card_num):
        
        nDigits = len(card_num)
        nSum = 0

        # isSecond is flag variable used to check if the selected position is Second digit or not
        isSecond = False
        
        # loop to iterate throughout the number
        for i in range(nDigits - 1, -1, -1):
                d = ord(card_num[i]) - ord('0')
        
                if (isSecond == True):
                        d = d * 2

                if d > 9:
                        d -= 9
                nSum +=d
                isSecond = not isSecond
        
        if (nSum % 10 == 0):
                return True
        else:
                return False

# Driver code 
if __name__=="__main__":
        card_num = input('Enter the card number\n')
        card_num = card_num.replace('.', '')

        # to check it a 16-digit number or not
        if len(card_num) < 16:
                print('This is not a credit card number')
                exit()

        #validation function call
        if (luhn(card_num)):
                print("This is a valid credit card")
        else:
                print("This is not a valid credit card")


Related Solutions

WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...
Hello! Please let me know, thank you! You have a credit card with a balance of...
Hello! Please let me know, thank you! You have a credit card with a balance of $13,600 and an APR of 18 percent compounded monthly. You have been making monthly payments of $260 per month, but you have received a substantial raise and will increase your monthly payments to $335 per month. How many months quicker will you be able to pay off the account? 37.39 Months 36.05 Months 40.06 Months 11.71 Months 34.33 Months Thank you!
Please write a basic function using Python. Please comment all steps. Thank you! Experimentally determined molecular...
Please write a basic function using Python. Please comment all steps. Thank you! Experimentally determined molecular structures are stored in the Protein Data Bank. Protein Data Bank format is a standard for files containing atomic coordinates which are stored in the “ATOM” record. Write a Python function to extract x coordinate of each atom for a given PDB file. Test your function with the provided “1a3d.pdb” file as the example. Also, give a good thought what would be the proper...
Using python, please complete these 4 exercises. Please limit your code to these skills: For loop...
Using python, please complete these 4 exercises. Please limit your code to these skills: For loop While loop Input function F strings Tuples Lists Nested ifs Elias Exercise 1 Using the following list, do the following: Sort it in ascending order In a print statement, referencing the list and its index numbers, print the lowest value and the highest value. [15, 70, 15, 38, 49, 98, 62, 89, 2, 21, 40, 74, 36, 36, 65, 1, 55, 16, 24, 56]...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the...
In python def lambda_2(filename): # Complete this function to read grades from `filename` and map the test average to letter # grades using map and lambda. File has student_name, test1_score, test2_score, # test3_score, test4_score, test5_score. This function must use a lambda # function and map() function. # The input to the map function should be # a list of lines. Ex. ['student1,73,74,75,76,75', ...]. Output is a list of strings in the format # studentname: Letter Grade -- 'student1: C' #...
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):...
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):     count=2;     flag=0;     # Loop to check the divisors of a number.     while(count<num and flag==0):         if(num%count!=0):             # Put flag=0 if the number has no divisor.             flag=0         else:             # Put flag=1 if the number has divisor.             flag=1         # Increment the count.         count=count+1     # Return flag value.     return flag # Intialize list. list=[]...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
I need java code for this ..thx Question 2 Credit card numbers are not completely random...
I need java code for this ..thx Question 2 Credit card numbers are not completely random sequences; they follow certain rules depending on the card issuer. A MasterCard number must meet these criteria: • Begin with 51, 52, 53, 54, 55, or something in the range 222100-272099 • 16 digits in length • Satisfy the Luhn formula, created by IBM scientist Hans Peter Luhn in the 1950s Here’s how the Luhn formula works: Double every other digit going backwards, starting...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT