Write a complete python script for this problem: Credit Card Number Check The la
ID: 3771399 • Letter: W
Question
Write a complete python script for this problem:
Credit Card Number Check
The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers. For simplicity, in this assignment we assume that the credit card number is only 8 digits.
Suppose the 8 digits credit card number is 43589795
Steps to check the validity:
1. Starting from the leftmost digit, double every other digit. Add all digits of the resulting numbers. For example, with the number given above,
? Every other digit starting from leftmost digit: 4 5 9 9
? Doubling the digits: 8 10 18 18
? Adding all digits in these values: 8+ 1+0+ 1+8+ 1+8 = 27
2. Get the sum of each of the digits that were not included in the preceding step. For example, with the number given above,
? Digits not included: 3 8 7 5
? Sum: 3+8+7+5 = 23
3. Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In above case, 27+23 = 50, so the number is valid.
Write a Python function called “validateCreditCard” that takes 8-digit credit card number as the input parameter (string value) and display whether that number is valid or not. For example,
validateCreditCard(“43589795”) will display “Valid credit card number”
validateCreditCard(“43589792”) will display “Invalid credit card number”
Note:
? Steps of validateCreditCard function can be given as follows:
def validateCreditCard(card):
1. Define a variable to store double digits and initialize to empty string. Say, double_digits = “”
2. Define a variable to store the sum of double digits and initialize to zero. Say, double_digit_sum = 0
3. Define a variable to store the sum of remaining digits and initialize to zero. Say, remaning_sum = 0
4. For each number i in range(0,8) do,
a. Convert card character at index i to integer. Say, int_digit
b. If i is divisible by 2,
i. Double the int_digit. Say double_int_digit. (That is, double_int_digit = int_digit*2)
ii. Convert double_int_digit to a string value and concatenate to double_digits (That is, double_digits += str(double_int_digit)
c. Otherwise,
i. Convert card character at index i to integer. Say, int_digit
ii. Add the int_digit to remaning_sum
5. For each character c in double_digits do, a. Convert c to integer. Say int_c b. Add int_c to double_digit_sum 6. Add the values of double_digit_sum and remaning_sum together and store in a variable. Say, steps_total.
7. Convert steps_total in to a string value. Say steps_total_str
8. If steps_total_str ends with substring “0”, a. Display “Valid credit card number”
9. Otherwise,
a. Display “Invalid credit card number”
In addition, write Python statements (in the same script) to take 8 digits from user as a string value (use raw_input function) and display whether that it is valid or not. If user enters less than or greater than 8 digits, display an error message.
A sample interaction when the script is run must look like follows. Item in black font color are response entered by the user.
For reference, this image is an example of how it should work:
Case 1: Enter 8-digit credit card number: 43589795 Valid credit card number Enter 8-digit credit card number 43589792 Inalid credit card number Case 3: Enter 8-digit credit card number: 43589 Error: Card number is not 8-digitExplanation / Answer
def validateCreditCard(card):
double_digits = ""
double_digit_sum = 0
remaning_sum = 0
for i in range(8) :
if(i%2==0) :
int_digit = int(card[i])
double_int_digit = int_digit*2
double_digits += str(double_int_digit)
else :
int_digit = int(card[i])
remaning_sum = remaning_sum + int_digit
for c in double_digits:
int_c = int(c)
double_digit_sum = double_digit_sum + int_c
steps_total = double_digit_sum + remaning_sum
#steps_total_str = str(steps_total)
if steps_total % 10 == 0 :
return 1
else :
return 0
card = str(raw_input())
if len(card) < 8 or len(card)> 8 :
print "Error: Card number is not 8 digit"
if(validateCreditCard(card)) :
print "valid credit card"
else :
print "invalid credit card"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.