Ask Question
1 June, 08:51

Credit card numbers follow a standard system. For example, Visa, MasterCard, and Discpver Card all have 16 digits, and the first digit serves to indicate the card brand. All Visa cards start with a 4; MasterCard cards always starts with a 5; and American Express cards start with a 2. Write a program that prompts for a 16-digit credit card and emits the name of the brand. Use the Try/Except/Else structure to ensure the user enters digits only. If the card brand cannot be determined or if the correct number of digits was not entered, the program should output "Unknown Card."

+2
Answers (1)
  1. 1 June, 11:36
    0
    try:

    cardNumber = str (input ('Enter your card number her: / n'))

    if (len (cardNumber) > 16 or len (cardNumber < 16)):

    raise

    except:

    print ('You have entered an invalid cardNumber.')

    else:

    if cardNumber. startswith ("2"):

    print ('American Express Card')

    elif cardNumber. startswith ("4"):

    print ('Visa Card')

    elif cardNumber. startswith ("5"):

    print ('Master Card')

    else:

    print ('Unknown Card')

    Explanation:

    In the try block section:

    The first line prompt the user for input, which is converted to string and assigned to cardNumber variable. The next line test the length of the cardNumber entered, if it is less than 16 or greater than 16; an exception is raise.

    In the except section:

    An error message is displayed telling the user that he/she has entered an invalid card number.

    In the else section:

    This is where is program check for type of card using an if ... elif ... else statement block. If the cardNumber start with 2; an output of "American Express card" is displayed. If the cardNumber start with 4; an output of "Visa card" is displayed. If the cardNumber start with 5; an output of "Master card" is display else "Unknown card" is displayed to the user.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Credit card numbers follow a standard system. For example, Visa, MasterCard, and Discpver Card all have 16 digits, and the first digit ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers