Skip to content

Commit b8d14f1

Browse files
committed
Added Python explanation
1 parent 0a6a412 commit b8d14f1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Python/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,47 @@ Read more:
2525
- [What is Checksum on a credit card?](https://www.sapling.com/7966257/checksum-credit-card)
2626

2727
### Python solution
28+
The Python solution is very similar to the pseudocode written in generalised solution of `README.md`.
29+
There are some Python specific functions/methods used which makes the code a lot more compact.
2830

31+
To begin with, we get the credit card number from the user and instantly convert it to a list.
32+
We then use `pop()`, a function that removes, and then returns, the last element of a list.
33+
We assign the return of `pop()`, the last number in `credit_card_number`, to `last_digit`.
34+
```python
35+
# Get input, define last_digit, and remove the 16th digit from the credit card.
36+
credit_card_number = list(input("Please enter the credit card number: "))
37+
last_digit = credit_card_number.pop()
38+
```
39+
40+
Next is the meat of our code.
41+
We use `enumerate()` which, in Python, gives us both `i` the current index and `number` the current value at that index (e.g. the current number in `credit_card_number`).
42+
For each number in `credit_card_number` we do the following:
43+
1. Check if the index `i` of the `number` is even. If so we multiply that number by 2.
44+
2. We check if the current number is longer than 1 digit. If so we break up the number and add each digit together.
45+
3. Ensure every number in `credit_card_number` is an integer and not a string because later on we use `sum()` which doesn't like strings.
46+
```python
47+
# Make use of enumerate to get the index and value of each number in the credit card number.
48+
for i, number in enumerate(credit_card_number):
49+
if i % 2 == 0: # if the index is even (i.e. every other element)
50+
credit_card_number[i] = int(number) * 2
51+
52+
# Check is the length of the number is more than a single digit.
53+
if len(str(credit_card_number[i])) > 1:
54+
# Break up the number into two digits and add them together
55+
# Example: 12 := 1 + 2 = 3
56+
x = str(credit_card_number[i])[0]
57+
y = str(credit_card_number[i])[1]
58+
credit_card_number[i] = int(x) + int(y) # We replace the previous number with our new single digit one.
59+
60+
# Here we make sure every element is converted to an integer otherwise we have issues with sum() later on.
61+
credit_card_number[i] = int(credit_card_number[i])
62+
```
63+
64+
Finally, we check if the `credit_card_number` is valid.
65+
We add the sum of the `credit_card_number` and add the `last_digit` to it.
66+
We then check if the result is a multiple of 10 using `% 10 === 0`.
67+
```python
68+
# Add up all the numbers, add the 16th digit, then check if final number is a multiple of 10.
69+
is_valid = (sum(credit_card_number) + int(last_digit)) % 10 == 0
70+
print(f"Is credit card valid: {is_valid}") # Output answer to user: True or False.
71+
```

0 commit comments

Comments
 (0)