+ 1

Credit Card Validator

You need to verify if the given credit card number is valid. For that you need to use the Luhn test. Here is the Luhn formula: 1. Reverse the number. 2. Multiple every second digit by 2. 3. Subtract 9 from all numbers higher than 9. 4. Add all the digits together. 5. Modulo 10 of that sum should be equal to 0. Task: Given a credit card number, validate that it is valid using the Luhn test. Also, all valid cards must have exactly 16 digits. Input Format: A string containing the credit card number you need to verify. Output Format: A string: 'valid' in case the input is a valid credit card number (passes the Luhn test and is 16 digits long), or 'not valid', if it's not. https://code.sololearn.com/cg9Gse7F2MQ9/?ref=app

5th Jan 2023, 2:11 PM
Soumya Ranjan Behera
Soumya Ranjan Behera - avatar
5 Answers
+ 7
Soumya Ranjan Behera , not clear for me what your issue is. can you give a short description of your problem?
5th Jan 2023, 8:13 PM
Lothar
Lothar - avatar
+ 7
Soumya Ranjan Behera , thanks for posting the task descripting. but it does not answer what issue / error you have with your code.
6th Jan 2023, 3:44 PM
Lothar
Lothar - avatar
+ 2
You can try this code if not happy from yours: def is_valid_card(card_number): # Convert the card number to a string card_string = str(card_number) # Check if the card number is the correct length if len(card_string) != 16: return False # Initialize a variable to store the sum of the digits total = 0 # Iterate over the digits in the card number for i in range(len(card_string)): # Get the current digit digit = int(card_string[i]) # If it is an even index, double the digit and add the # resulting digits to the total if i % 2 == 0: digit *= 2 if digit > 9: digit = digit % 10 + digit // 10 total += digit # Return True if the total is divisible by 10, False otherwise return total % 10 == 0 # Test the function print(is_valid_card(1234567890123456)) # Output: False print(is_valid_card(1234567812345678)) # Output: True print(is_valid_card(1234567812345670)) # Output: False
6th Jan 2023, 6:58 PM
Aditya Dixit
+ 1
alot of crashes will happened
5th Jan 2023, 2:59 PM
Ion Kare
Ion Kare - avatar
+ 1
def ccvalid(inputs): inputs = list(inputs[::-1]) #every second value changed inputs[1::2] = [2*int(x) for x in inputs[1::2]] #check values >9 y = 0 for x in inputs: if int(x)> 9: y += int(x)-9 else: y += int(x) #check modulo 10 is 0 if len(inputs) == 16 and y % 10 == 0: print("valid") else: print("not valid") ccvalid(input())
8th Jan 2023, 4:59 AM
ken baumli
ken baumli - avatar