Credit card valudator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Credit card valudator

Can any one test my simple code for check the credit card validation https://code.sololearn.com/crsM7Cx3FUcK/?ref=app

30th Nov 2023, 7:13 AM
Mohammed Hassan
Mohammed Hassan - avatar
31 Answers
+ 1
Think about it this way. Hopefully, this can help you out. You clearly understand how the conditional operator is working, and the way you are using the OPERATOR is fine. But if you consider what it is you are comparing, think about where the value of 'i' is coming from. Each time the loop iterates, you are updating i. But where are you storing that value? It disappears on the next iteration. After you have looped through all the digits, you only make the comparison once. Can you see a difference between when the comparison takes place in your code and mine? Try writing it out in pseudocode first and understand what order things should be in. Ask me any questions you need along the way.
1st Dec 2023, 10:16 AM
StuartH
StuartH - avatar
+ 5
Mhommad Hassan If you want to pass the code coach, you should not put anything inside input(). Any extra character or space is considered as incorrect answer.
30th Nov 2023, 12:22 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 4
In the "Code Coach" under "Community" section, there is a challenge "Credit Card Validator". Your code seems following the rules. You can try your code in there.
30th Nov 2023, 9:07 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Mhommad Hassan, seriously? Is this your code?
30th Nov 2023, 10:36 AM
Solo
Solo - avatar
+ 2
Mhommad Hassan , explain how this code works...
30th Nov 2023, 11:37 AM
Solo
Solo - avatar
+ 2
Mhommad Hassan OK, I'm still looking forward to an explanation from you about the conditional expression...😎
1st Dec 2023, 7:57 AM
Solo
Solo - avatar
+ 1
Mhommad Hassan , there are mistakes in this code that can hardly be made meaningfully...
30th Nov 2023, 11:20 AM
Solo
Solo - avatar
+ 1
Mhommad Hassan You are trying to perform actions on strings which cannot be done. Strings are immutable remember, you cannot modify them. At line 9, you are also not feeding your new number back anywhere, sinply updating i in place which is lost on the next iteration Below is my suggestion, which works as follows: Accept input (prompt removed to comply with code coach) Reverse the string Iterate through the string converting each to an integer, performing actions and adding to total T Check modulus Output result. Its one solution and by no means the only way. See if you can modify your own code to convert string elements to int and allow your operations to succeed. T=0 n=input() n=n[::-1] for i in range(len(n)): if i!=0 and i%2==1: x = int(n[i])*2 if x > 9: x -= 9 else: x = int(n[i]) T += x if T%10==0 and len(n) == 16: print("valid") else: print("not valid")
30th Nov 2023, 12:54 PM
StuartH
StuartH - avatar
+ 1
The key takeaways are that you cannot modify digits in a string, and that updating i in your context isn't updating your original element just the value of i for that iteration.
30th Nov 2023, 1:07 PM
StuartH
StuartH - avatar
+ 1
Mhommad Hassan I understand you're talking about the i of the cycle: for i in n[1::2]: i=int(i)*2 What does the if i>9 condition check for you?😎
30th Nov 2023, 4:06 PM
Solo
Solo - avatar
+ 1
Mhommad Hassan consider my post above alongside what Solo is saying and you’ll see the issue. i is assigned a value in the loop, but as soon as that iteration of the loop ends it is destroyed. The new value of i you create in the loop doesn't update the string the way you think it does and you can't call it later in your code.
30th Nov 2023, 4:13 PM
StuartH
StuartH - avatar
+ 1
Okay Suppose X=0 If i>9: i=i-9 x=x+i Mhommad Hassan if this is addressed to me, then you don't have to assume anything, I already know how the code works. The questions that I ask you must answer to yourself. Describe in words what this condition checks in your code, if you do it correctly, you will understand your main mistake and you can move on to the next one, you have a lot of them. Of course, this is if you want to learn how to program, and not just copy other people's codes. Because if you don't understand this mistake, then there is no point in discussing the solution of this Task, you just need to start learning the course from the beginning.
30th Nov 2023, 4:44 PM
Solo
Solo - avatar
+ 1
Solo off course no gain without pain The context of course is not enough to be familiar with programing it requir more practice
30th Nov 2023, 4:58 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Mhommad Hassan you're right, but unfortunately in our world everything is born in pain. But this mistake is so worthless that at first it may seem like a typo, but since you still don't see it, then you really better reread the very first lessons and in particular about the cycle. When you understand what's going on, you'll laugh at the fact that you've spent so much time on this problem. That's why I asked my first question. Is this your code? You answered Yes. Now I have one more question. Is this your account? The first code from this account was created 7 years ago, then for 3 years there was not a single written code and now the code appears again after 4 years of inactivity. I apologize for my incredulity, but you must agree that I have good reason to do so.
30th Nov 2023, 10:32 PM
Solo
Solo - avatar
+ 1
Solo yes this my office account but trully is iam not connecting with my account since 2015 , yes i made refresh for main rules but i will continue to learn
1st Dec 2023, 5:51 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Solo Conditional expression It is a way to ensure that the variable, input, or assigned value is compatible with the goal of creating the program So that the program's path is determined if the condition is met, and if it is not met, the program behaves differently
1st Dec 2023, 8:56 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Mhommad Hassan did you copy the definition of a conditional expression from the course?-) Well, in your own words about your conditional expression. Does it ensure that the variable, input data, or assigned value is compatible with the purpose of creating the desired program?-)
1st Dec 2023, 9:48 AM
Solo
Solo - avatar
0
Solo can you tell me the wrong
30th Nov 2023, 11:00 AM
Mohammed Hassan
Mohammed Hassan - avatar
0
Wong Hei Ming i try it not fully is succeed
30th Nov 2023, 11:06 AM
Mohammed Hassan
Mohammed Hassan - avatar
0
Solo Ok write to be the mistakes in list plz
30th Nov 2023, 11:26 AM
Mohammed Hassan
Mohammed Hassan - avatar