Search In
• More options...
Find results that contain...
Find results in...

# Card verification

```def digit_sum(LIST):
for i in range(len(LIST)):
value = str(LIST[i])
digits = [int(x) for x in value]
credit_card.append(sum(digits))

def checker(s):
return s.replace(' ','').replace('-','')

credit_card=[]
credit_card_sum=[]
credit_card_sum_digit=[]
credit_digits_sum=0

user_input=input()
checker(user_input)
while user_input!="end" and user_input!="END":
credit_card_int=[int(x) for x in user_input]
if credit_card_int[0]<4 or credit_card_int[0]>=7:

print ("False")
else:
for i in range(len(credit_card_int)):
if not (i% 2):
credit_card_int[i] = credit_card_int[i]*2
digit_sum(credit_card_int)
for i in range(len(credit_card)):
credit_digits_sum=credit_digits_sum+credit_card[i]
if credit_digits_sum % 2 ==0:
print ("True")
else:
print ("False")
user_input=input()
```

Can you guys help me make the program output "False" when it detects on the user input asterisks,commas and dots? And also letters? I keep messing it up. If i manage to make something i will update!

##### Share on other sites

@mshaugh Yea,what if the user enters  5497-3571-7063-2130 .Thats a valid credit card. I must get True for output

##### Share on other sites

Check if checker(user_input) is a digit.

##### Share on other sites

Its more complicated  from this

##### Share on other sites

This will simply confirm that all of the user's input are digits, without any other characters.

##### Share on other sites

The other characters for example "-" are acceptable in this case. So it must continue,not output false!

##### Share on other sites

1 hour ago, FreQUENCY said:

The other characters for example "-" are acceptable in this case. So it must continue,not output false!﻿

Isn't your checker method already removing all "-" from the user input? One approach could just be having your checker method take the user input and handle all of the valid but non-digit cases (such as what it's already doing for spaces and "-") and so that no matter what the user types in, as long as it's valid (such as xxxx-xxxx-xxxx-xxxx) it will still end up as a digit only string.

Also, this logic could be broken up quite a bit. I'd break this up into separate functions that each handle a different validation, such as

```def validateFirstDigit(card_number):
return card_number[0] < 4 and card_number[0] < 7```

It's a bit less efficient since you'll probably have to iterate through the card numbers more than just once, however given this use case the tradeoff of readability and scalability (e.g. as more validations are needed) at the cost of a SLIIIIIGHT performance hit seem incredibly worth it, since it seems that this will only be run once per user input, and will only have a max length to work with of 16, so even if you had some ridiculously inefficient O(n3) or something, it wouldn't affect the user experience at all (and going through the numbers more than once doesn't exponentially increase time anyway)

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

##### Share on other sites

```def digit_sum(LIST):
for i in range(len(LIST)):
value = str(LIST[i])
digits = [int(x) for x in value]
credit_card.append(sum(digits))

def checker(s):
list_of_numbers=[]
string_to_use=""
if("-" in s):
list_of_numbers=s.split("-")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
if(" " in s):
list_of_numbers=s.split(" ")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
if("," in s):
list_of_numbers=s.split(",")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
if("*" in s):
list_of_numbers=s.split("*")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return

if("." in s):
list_of_numbers=s.split(".")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
else:return s

credit_card=[]
credit_card_sum=[]
credit_card_sum_digit=[]
credit_digits_sum=0

user1_input=input()
user_input=checker(user1_input)
while user1_input!="end" and user1_input!="END":
user_input=checker(user1_input)
if( not user_input.isdigit()):
print("False")
elif(user_input.isdigit() and len(user_input)>16):
print("False")
else:
credit_card_int=[int(x) for x in user_input]
if credit_card_int[0]<=3 or credit_card_int[0]>=7:
print ("False")
else:
for i in range(0,len(credit_card_int),2):
credit_card_int[i] = credit_card_int[i]*2
digit_sum(credit_card_int)
for i in range(len(credit_card)):
credit_digits_sum=credit_digits_sum+credit_card[i]
if credit_digits_sum % 10 ==0:
print ("True")
else:
print ("False")
user1_input=input()
```

Changed to this!. Outputs fail bool when entering

`5497 3571 7063 2130  @reniat`

##### Share on other sites

25 minutes ago, FreQUENCY said:

Changed to this!. Outputs fail bool when entering

```
5497 3571 7063 2130﻿﻿  @reniat```

hmmm.. This code outputs True for that input for me...

Do you know which condition it's failing? Can you step-through the program in debug mode?

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

##### Share on other sites

```def checker(s):
s = s.replace(' ', '').replace('-', '')
return s.isdigit()

checker('1234123412341234') # True
checker('1234-1234-1234-1234') # True
checker('1234 1234 1234 1234') # True
checker('*234 1234 1234 1234') # False```

##### Share on other sites

done!

```

def checker(s):
list_of_numbers=[]
string_to_use=""
if("-" in s):
list_of_numbers=s.split("-")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
if(" " in s):
list_of_numbers=s.split(" ")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
if("," in s):
list_of_numbers=s.split(",")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
if("*" in s):
list_of_numbers=s.split("*")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return

if("." in s):
list_of_numbers=s.split(".")
for character in list_of_numbers:
string_to_use+=character
string_return=string_to_use
string_to_use=""
return string_return
else:
return s

credit_card=[]
credit_card_sum=[]
credit_card_sum_digit=[]
credit_digits_sum=0

user1_input=input()
user_input=checker(user1_input)
check=False
while user1_input!="end" and user1_input!="END":
user_input=checker(user1_input)
if(len(user1_input)>16):
if(len(user1_input)!=19):
print("False")
user1_input=input()
continue
elif(user1_input[4].isdigit() or user1_input[9].isdigit() or user1_input[14].isdigit()):
print("False")
user1_input=input()
continue
if( not user_input.isdigit()):
print("False")
elif(user_input.isdigit() and len(user_input)!=16):
print("False")
else:
credit_card_int=[int(x) for x in user_input]
if credit_card_int[0]<4 or credit_card_int[0]>7:
print ("False")
else:
for i in range(0,len(credit_card_int),2):
credit_card_int[i] = credit_card_int[i]*2
if credit_card_int[i]>=10:credit_card_int[i]= (int(str(credit_card_int[i])[0])+int(str(credit_card_int[i])[1]))
if (sum(credit_card_int) % 10 ==0):
print ("True")
else:
print ("False")
user1_input=input()

```