+ 1
What's wrong with my code
Im trying to solve credit card verification challenge but last 2 cases wrong: cc=input() c="" if cc.isnumeric() and len(cc)==16: for i in cc: c=i+c cl=list(c) for i in range(1,16,2): cl[i]=int(cl[i])*2 for i in range(16): if int(cl[i])>9: cl[i]=int(cl[i])-9 sum=0 for i in cl: sum=sum+int(i) valid=sum%10 if valid==0: print("valid") else: print("not valid")
6 Respostas
+ 1
Not checked fully but you don't have else part for if input length is not equal to 16
Add else part to print not valid then try again
+ 2
Yes worked,thank you
Cant believe i was stuck because of this hahaha
+ 2
#check this out
cc = input()
if not cc.isdigit():
print('Not valid')
elif len(cc) != 16:
print('Not valid')
else:
print('Valid
+ 1
Pls Save code and share link with the task description..
+ 1
https://code.sololearn.com/cBXMjbvRF4s4/?ref=app
Task desc:
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.
Sample Input:
4091131560563988
+ 1
Yes that was the problem in that code