Credit Card number check using loops in Python?

Page 1 of 1

3 Replies - 418 Views - Last Post: 18 September 2015 - 12:34 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=381806&amp;s=35507d98c49ea479d6400aeed7448b25&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 joshuacheung8

Reputation: 0
• Posts: 2
• Joined: 18-September 15

Credit Card number check using loops in Python?

Posted 18 September 2015 - 10:57 AM

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 but, for simplicity, we will describe it for numbers with 8 digits instead of 16:

• Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 4358 9795, then you form the sum 5 + 7 + 8 + 3 = 23.

• Double each of the digits that were not included in the preceding step. Add all digits of the resulting numbers. For example, with the number given above, doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.

• Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid.

Write a program that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. If it is not valid, you should print the value of the check digit that would make it valid.

I have to use loops to do the sum. However, I do not know how to use loops for that.

Here's my code

```card = input("Enter your credit card number here: ")
digit_sum = 0

for i, digit in enumerate(reversed(card)):
n = int(digit)

if i % 2 == 0:
digit_sum += n
elif i >= 5:
digit_sum += n * 2 - 9
else:
digit_sum += n * 2
if digit_sum % 10 == 0:
print("Card is valid!")
else:
print("Card is invalid!")

```

Is This A Good Question/Topic? 0

Replies To: Credit Card number check using loops in Python?

#2 DK3250

Reputation: 55
• Posts: 189
• Joined: 27-December 13

Re: Credit Card number check using loops in Python?

Posted 18 September 2015 - 12:23 PM

You have a small error in line 9
If you correct this, your code works.

You use a for loop - so why claim you don't understand it ?
Do you have a more specific question ?

This post has been edited by DK3250: 18 September 2015 - 12:29 PM

#3 joshuacheung8

Reputation: 0
• Posts: 2
• Joined: 18-September 15

Re: Credit Card number check using loops in Python?

Posted 18 September 2015 - 12:28 PM

It works without it as well. I redid the code. My main concern is adding loops to the code.

#4 DK3250

Reputation: 55
• Posts: 189
• Joined: 27-December 13

Re: Credit Card number check using loops in Python?

Posted 18 September 2015 - 12:34 PM

No, you need to correct line 9 to get the code right.