here is my code . Need help to get it right. i am using python 3. cardNumber = i
ID: 3793429 • Letter: H
Question
here is my code . Need help to get it right. i am using python 3.
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')
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 ("valid card")
else:
print ("Not valid")
HERE THE ASSIGNMENT
''''''''
,,,,
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: must start with 4 Visa cards: MasterCard must start with 5 must start with 37 o American Express: must start with 6 o Discover 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 7 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 alid Discover 6312345678901 Valid Visa 38857 Valid Am Ex Valid MastercardExplanation / Answer
In the code you have missed decrementing "i" in the function sumDblEvenPlace. That's y it is going into loop. I have also added the necessary information for the output. The following is the code.
Code:
#!/usr/bin/python3
dd={"len": "Too Short", "len2":"Too long", "len3":"Valid 13-16 digit entry", "First":"First character is not valid"};
cardNumber = input('Please Enter card Number ');
length = len(cardNumber);
print ("CardNumber:"+cardNumber)
print (length)
if(cardNumber[0] == '4'):
print ("Prefix: Visa")
elif(cardNumber[0] == '5'):
print ("Prefix: MasterCard")
elif(cardNumber[0] == '3' and cardNumber[1] == '7'):
print ("Prefix: American Express")
elif(cardNumber[0] == ''):
print ("Prefix: Discover")
def IsValidLength(card):
sz = len(card)
print("Length:"+str(sz));
if(sz >= 13 and sz <= 16):
return True
else:
return False
#sum of double even place digits
def sumDblEvenPlace(card):
total =0
sz = len(card)
sumstr="(";
i=sz-2
while(i>=0):
num=ord(card[i])-ord('0')
tmpnum=0;
num=num*2
if(num > 9):
adder=0
while(num>0):
r=num%10
adder=r
tmpnum+=adder
num=num//10
total = total + adder
if(i==0):
sumstr+=str(tmpnum);
else:
sumstr+=str(tmpnum)+"+";
else:
total = total + num
sumstr+=str(num)+"+";
i=i-2
sumstr+=")";
print("SumDblEvenPlace:"+str(total)+sumstr);
return total
#sum of odd place digit
def sumOddPlace(card):
total = 0
sz=len(card)
i=sz-1
sumstr="(";
while(i>=0):
num=ord(card[i])-ord('0')
#print ("num ",num)
if(i==0):
sumstr+=str(num)
else:
sumstr+=str(num)+"+";
total = total + num;
i=i-2;
sumstr+=")";
print("sumOddPlace: "+str(total)+" "+sumstr);
return total
def validCard(Tot):
if(Tot % 10 == 0):
return True
else:
return False
validity = 1
a=1;
b=2;
if(IsValidLength(cardNumber)):
a = sumDblEvenPlace(cardNumber)
b = sumOddPlace(cardNumber)
S = a + b
if(validCard(S)):
print ("Valid: Yes")
else:
print ("Valid: No")
Output:
Please Enter card Number
4388576018410707
CardNumber:4388576018410707
16
Prefix: Visa
Length:16
SumDblEvenPlace:29(0+0+8+2+3+1+7+8+)
sumOddPlace: 41 (7+7+1+8+0+7+8+3+)
Valid: Yes
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.