Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON FUNCTION: PLEASE SERIOUS PROGRAMER AND FOLLOW INSTRUCTIONS . THANKS!! Pro

ID: 3783096 • Letter: P

Question

PYTHON FUNCTION: PLEASE SERIOUS PROGRAMER AND FOLLOW INSTRUCTIONS . THANKS!!

Program 2 (20 pts) Fall 2016 Credit Card Number Validation: The Luhn check or the Mod 10 check

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 or scanned correctly. To ensure validity:

Check to see that the number has 13-16 digits

For certain companies, must start with a particular number:

Visa cards: must start with 4

Mastercard: must start with 5

American Express: must start with 37

Discover: must start with 6

Going from RIGHT to LEFT, double every second digit. If doubling a digit results in a two-digit number, add up the two digits to get a single-digit number.

Then ADD all of these single-digit numbers together  sumSecondDigits.

Next, going from RIGHT to LEFT, add all digits in the ODD places  sumOddPlaces.

Add the sumSecondDigits to the sumOddPlaces.
If the result is divisible by 10, the card number is valid. Otherwise, it is invalid.

Write a function called isValidCreditCard that reads accepts a string and returns True if the string represents a valid credit card number and False if it is invalid.


NOTE: the function MUST call other functions to complete its work. You should create at LEAST the following functions:

A function that accepts a string and returns the sum of the digits in the even places doubled

A function that accepts a string and returns the sum of the digits in odd places

A function that accepts a string and returns the Prefix as a string (Visa, MC, Discover, AmEx).

Others as you see fit

Step 2: Create a list of sample credit card numbers and call your function, printing the number and a message indicating if the number was valid or not. Here are some ideas about what to test:

Too long

Too short

Valid 13-16 digit entry

First character is a letter

Beginning characters not valid (starts with letter, starts with # other than 4, 5, 6 or 37)

Sum of sums not evenly divisible by 10

Valid Discover # 6312345678901

Valid Visa # 4388576018410707

Valid AmEx #

Valid Mastercard #

=========================================================================================

Example1:

CardNumber: 4388576018402626
Length: 16
Prefix: Visa
sumDblEvenPlace: 37

2*2 = 4, 2*2=4, 4*2 = 8, 1*2 = 2, 6*2 = 12 two digits, so add them and get 1+2 =3, 5*2 = 10 two digits, so add them and get 1+ 0 =1, 8*2 = 16 two digits, so add them and get 1+6 =7, 4*2 = 8
Sum: (4 + 4 + 8 + 2 + 3 + 1 + 7 +8)  37

sumOddPlace: 38 ( 6+6+0+8+0+7+8+3)
Valid?: No

Example2:

CardNumber: 4388576018410707
Length: 16
Prefix: Visa
sumDblEvenPlace: 29 ( 0 + 0+ 8 + 2 + 3 + 1 + 7 + 8 = 29)
sumOddPlace: 41
7 + 7 + 1 + 8 + 0 + 7 + 8 + 3 = 41
Valid: Yes (29 + 41 = 70 which is evenly divisible by 10)

Explanation / Answer

# python code

cardNumber = input('Please Enter card Number ');
length = len(cardNumber);

print (cardNumber)
print (length)

if(cardNumber[0] == '4'):
   print ("Visa")
elif(cardNumber[0] == '5'):
   print ("MasterCard")
elif(cardNumber[0] == '3' and cardNumber[1] == '7'):
   print ("American Express")
elif(cardNumber[0] == ''):
   print ("Discover")
  
def IsValidLength(card):
sz = len(card)
if(sz < 13 and sz > 16):
return 'false'
else:
return 'true'
      
#sum of double even place digits
def sumDblEvenPlace(card):
   total =0
   sz = len(card)
   i=sz-2
   while(i>=0):
       num=ord(card[i]) - ord('0')
       num = num * 2
       if(num > 9):
           adder=0
           while(num>0):
           r=num%10
           adder=r
           num=num//10
           total = total + adder
       else:
           total = total + num
       i=i-2
   return total;

#sum of odd place digit
def sumOddPlace(card):
   total = 0
   sz=len(card)
   i=sz-1
   while(i>=0):
       num=ord(card[i])-ord('0')
       #print ("num ",num)
       total = total + num;
       i=i-2;

   return total;
          
  
def validCard(Tot):
   if(Tot % 10 == 0):
       return 'true'
   else:
       return 'false'
  
validity = 1
if(IsValidLength(cardNumber)):
a = sumDblEvenPlace(cardNumber)
b = sumOddPlace(cardNumber)
print (a)
print (b)
S = a + b
print (S)
if(validCard(S)):
print ("Yes")
else:
validity=0
print ("No")
else:
if(validity==1):
print ("No")