-1

I'm making a program that validates a credit card, by multiplying every other number in the card number by 2; after i'll add the digits multiplied by 2 to the ones not multiplied by 2. All of the double digit numbers are added by the sum of their digits, so 14 becomes 1+4. I have a photo below that explains it all. I'm making a python program that does all of the steps. I've done some code below for it, but I have no idea what to do next? Please help, and it would be greatly appreciated. The code I have returns an error anyway.

enter image description here

class Validator():
    def __init__(self):
        count = 1
        self.card_li = []
        while count <= 16:
            try:
                self.card = int(input("Enter number "+str(count)+" of your card number: "))
                self.card_li.append(self.card)
                #print(self.card_li)
                if len(str(self.card)) > 1:
                    print("Only enter one number!")
                    count -= 1
            except ValueError:
               count -= 1   
        count += 1
        self.validate()

    def validate(self):
        self.card_li.reverse()
        #print(self.card_li)
        count = 16
        while count >= 16:
            self.card_li[count] = self.card_li[count] * 2
            count += 2






Validator()
George Baker
  • 91
  • 1
  • 3
  • 13
  • 4
    There are so many things wrong with this code... Do us all a favor and read this: [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) And then take a long, hard look at all your loops and list indices... – Aran-Fey Jul 15 '16 at 19:02

2 Answers2

3

To perform that sum:

>>> s = '4417123456789113'
>>> sum(int(c) for c in ''.join(str(int(x)*(2-i%2)) for i, x in enumerate(s)))
70

How it works

The code consists of two parts. The first part creates a string with every other number doubled:

>>> ''.join(str(int(x)*(2-i%2)) for i, x in enumerate(s))
'8427226410614818123'

For each character x in string s, this converts the character to integer, int(x) and multiplies it by either 1 or 2 depending on whether the index, i, is even or odd: (2-i%2). The resulting product is converted back to a string: str(int(x)*(2-i%2)). All the strings are then joined together.

The second part sums each digit in the string:

>>> sum(int(c) for c in '8427226410614818123')
70
John1024
  • 109,961
  • 14
  • 137
  • 171
0

You need to increment the count inside the while() loop. Also, append the user input to your card_li list after you have the if.. check. Your init method should look like:

def __init__(self):
    count = 1
    self.card_li = []
    while count <= 16:
        try:
            self.card = int(input("Enter number "+str(count)+" of your card number: "))
            if len(str(self.card)) > 1:
                print("Only enter one number!")
            self.card_li.append(self.card)
            count += 1
        except ValueError:
           pass  
    self.validate()

As for your validate method, it doesn't seem complete even according to the logic you have written.

Ankur Gupta
  • 707
  • 1
  • 9
  • 20