Question

Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

Using the above described algorithm, create a program that: (IN PYTHON)

1.Asks the user which type of credit card he/she would like to find the checksum for.

2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.]

3. Using the user's input of the n digits, finds the last digit of the sum.

4. Displays the resulting credit card i.e.; AMEX, MASTERCARD, VISA, or INVALID

According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows:

1. Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.

2. Add the sum to the sum of the digits that weren’t multiplied by 2.

3. If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid! That’s kind of confusing, so let’s try an example with American Express: 378282246310005

let’s first underline every other digit, starting with the number’s second-to-last digit: 378282246310005

let’s multiply each of the underlined digits by 2: 7*2 + 2*2 + 2*2 + 4*2 + 3*2 + 0*2 + 0*2

That gives us: 14 + 4 + 4 + 8 + 6 + 0 + 0 Now let’s add those products’ digits (i.e., not the products themselves) together: 1 + 4 + 4 + 4 + 8 + 6 + 0 + 0 = 27

Now let’s add that sum (27) to the sum of the digits that weren’t multiplied by 2: 27 + 3 + 8 + 8 + 2 + 6 + 1 + 0 + 5 = 60 Yup,

the last digit in that sum (60) is a 0, so the card is valid! So, validating credit card numbers isn’t hard, but it does get a bit tedious by hand.

Let’s write a program. Write a program that prompts the user for a credit card number and then reports whether it is a valid American Express, MasterCard, or Visa card number, per the definitions of each’s format herein.

your program’s last line of output be AMEX or MASTERCARD or VISA or DISCOVER or DINERS CLUB/CARTE BLANCHE or INVALID EXAMPLES Here are a few examples (user input is in bold):

Number: 378282246310005

AMEX

Number: 6175230925

INVALID

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Program

#Function to find Digits Sum
def productSum(num):
    product=0
    list_of_ints = [int(i) for i in str(num)]
    for i in list_of_ints:
        product+=i
    return product
#Function to check validity of card
def isValid(creditCardNum):
    #Length check
    if(len(creditCardNum)!=15):
        return False
    else:
        sum=0
        #Odd index part calculation
        for i in range(1,len(creditCardNum),2):
            sum+=productSum(int(creditCardNum[i])*2)
        #Even index
        for i in range(0,len(creditCardNum),2):
            sum+=int(creditCardNum[i])
        #Validity check
        if(sum%10==0):
            return True
        else:
            return False
#Main function
def main():
    #Prompt for input
    num=input('Number: ')
    #Is valid then display which type
    if(isValid(num)):
        if(num[0]=='4'):
            print('VISA')
        elif(num[0]=='5'):
            print('MASTERCARD')
        elif(num[0]=='6'):
            print('DISCOVER')
        elif(num[0]=='3'):
            if(num[1]=='4'or num[1]=='7'):
                print('AMEX')
            if(num[1]=='0'or num[1]=='6'or num[1]=='8'):
                print('DINERS CLUB/CARTE BLANCHE')
        else:
            print('INVALID')
     #Otherwise Invalid
    else:
        print('INVALID')
if __name__=='__main__':
    main()

-----------------------------------------------------------------------------

Output

Number: 378282246310005
AMEX

Add a comment
Know the answer?
Add Answer to:
Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • Write a program in Python that asks the user for the ISBN of the book in...

    Write a program in Python that asks the user for the ISBN of the book in the format xxx-x-xxx-xxxxx-x. To validate: Initialize a total to 0 Have the program remove the dashes so that only the 13 digits are left in the ISBN string for each index of the first 12 digits in the 13 digit ISBN string get the digit at the current index as an integer If the current index is an even number, add the digit to...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

  • Please write c++ (windows) in simple way and show all the steps with the required output

    please write c++ (windows) in simple way and show all the steps with the required output Write a C++ program that simulates the operation of a simple online banking system The program starts by displaying its main menu as shown in Figure1 splay Account nformatson verity Your credit Card elect vour choice Figure 1 Main menu of the program The menu is composed of the following choices 1. When choice 1 is selected (Display Account information), the program should display...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT