Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Credit card number validation Program Credit card numbers follow ce rtain patterns: It must have betwe en 13 and 16 digits, and the number must

Credit card number validation Program Credit card numbers follow ce rtain patterns: It must have betwe en 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 propos ed 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 is scanned correctly by a scanner. Credit card numbers are generated following this valid ity check, commonly known as the Luhn check or the Mod 10 check , which can be described as follo ws (for illustration, consider the card number 4388576018402626): 1. Double every second digit fr om right to left. If doubling of a digit results in a two- digit number, add up the two digits to get a single-digit number. 2. Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 3. Add all digits in the odd plac es from right to left in the c ard number. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38 4. Sum the results fr om Steps 2 and 3. 37 + 38 = 75 5. If the result from Step 4 is divisible by 10, the card numbe r is valid; otherwise, it i s invalid. For example, the number 4388 576018402626 is invalid, b ut the numbe r 438857601841 0707 is valid. Write a program that prompts the user to enter a credit card nu mber as a STRING . Allow the user to check multiple credit numbers until us er decides to quit. Prompt the user for how many credit card numbers he/she wants to check. You are to use the two credit card numbers prov ided in the sample output given below plus other credit card numbers using credit numbers that starts with 5, 37, and 6. Make sure y ou test at least 10 credit card numbers. Display whether the number is va lid or invalid. Design your pro gram to use the following functions ( include type annotations): def main(): #user will input the credit c ard number as a string #call the function isValid () and print whether the credit card number is valid or not valid def isValid(number): # Return true if the card number is valid #hint you will have to call function sumOfDoubleEvenPlace() and sumOfOddPlace def sumOfDoubleEvenPlace(number): # Get the result from Step 2 def getDigit(number): # Return this number if it is a single digit, otherwise, return # the sum of the two digits def sumOfOddPlace(number): # Return sum of odd place digits in number Here are the two sample runs using the credit card numbers abov e: >>> ============ RESTART: E:/ /Homework/HW3/HWP_3CC.py ============ Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Plus 8 test cases more with credit cards starting with 5, 37, and 6, testing for valid and not valid for each type of credit card type. >>> Some notes to help with Program Set 3 Searching for Substrings You can search for a substring in a string by using the methods below: Example: 1 >>> s = "welcome to python" 2 >>> s.endswith("thon") 3 True 4 >>> s.startswith("good") 5 False 6 >>> s.find("come") 7 3 8 >>> s.find("become") 9 -1 10 >>> s.rfind("o") 11 17 12 >>> s.count("o") 13 3 14 >>> Since come is found in string s at index 3 , s.find("come") returns 3 (line 7). Because the first occurrence of substring o from the right is at index 17, s.rfind("o") returns 17 (line 11). In line 8, s.find("become") returns -1 , since become is not in s . In line 12, s.count("o") returns 3 , because o appears three times in s . Here is another example : s = input( " Enter a string: " ) if s.startswith( " comp " ): print(s, " begins with comp " ) if s.endswith( " er " ): print(s, " ends with er " ) print( ' e ' , " appears " , s.count( ' e ' ), " time in " , s) >>> If you enter computer when running the code, it displays computer begins with comp computer ends with er e appears 1 time in computer

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions