Simple passcode checker | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Simple passcode checker

OK, this is racking my brain. Ive stripped this code back down to the bare bones for this question. How do i get the code to only print "Valid Code" only once, meaning after it has checked all numbers? Example, if i type 12345, i dont want it to say "Valid Code" 5 times, only once. I did put a break in the "Invalid" part, because as soon as it sees 67890, there is no need to continue checking the rest of the numbers. Keep in mind, i fully understand why i get this output with the current "print" statements i have show. I have tried to put it at the end, but if i did an "Invalid" entry, it would still print "valid" after the code ran. This is nothing real, and only for my own learning. Thanks https://code.sololearn.com/cpo3349NtS4w/?ref=app

11th Jun 2019, 3:07 PM
Beau Tooley
Beau Tooley - avatar
13 Answers
+ 6
You actually don't need to change your code much. What you desire can simply be obtained using a boolean. https://code.sololearn.com/cGjTpwzLP71Q/?ref=app
11th Jun 2019, 9:24 PM
Kainatic [Mostly Inactive Now]
Kainatic [Mostly Inactive Now] - avatar
+ 5
def numbercheck(): code = input("Enter a number- ") if all(n in '12345' for n in code): print('Valid Code') elif any(n in '67890' for n in code): print('Invalid Code') numbercheck() Note that this won't output anything if you enter something like "fd f hjjh" and the elif statement is technically not necessary if the input is guaranteed to be a number
11th Jun 2019, 3:43 PM
Anna
Anna - avatar
+ 4
NUKE Yes, it is incorrect. The output should only have ONE statement - either "Valid code" or "Invalid code" - based on the condition.
26th Jun 2019, 6:06 PM
Kainatic [Mostly Inactive Now]
Kainatic [Mostly Inactive Now] - avatar
+ 3
NUKE It doesn't. It sees the first character ('4'), prints "Valid Code" and leaves the loop without checking any of the other characters
26th Jun 2019, 11:48 AM
Anna
Anna - avatar
11th Jun 2019, 3:25 PM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 2
NUKE , that would not work. If the first number is good, it breaks out and finishes. If you input 456, it would still show good even though a 6 is present.
26th Jun 2019, 2:23 AM
Beau Tooley
Beau Tooley - avatar
+ 1
Just don't use loop => try it with if-elif construction and string comparison. Please look at the code. https://code.sololearn.com/cjfdbB80i6vK/?ref=app. Hope it helps you 😉
11th Jun 2019, 3:23 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
27th Jun 2019, 5:35 AM
Ankit Tiwari
Ankit Tiwari - avatar
0
It's tough
11th Jun 2019, 5:33 PM
Justin
Justin - avatar
0
def numbercheck(): code = input("Enter a number- ") for x in (code): if x in ("12345"): print("Valid Code") break #just place break here and u good bro! elif x in ("67890"): print ("Invalid Code") numbercheck()
25th Jun 2019, 6:15 AM
Ankit Tiwari
Ankit Tiwari - avatar
0
Anna I am getting Valid code Valid code Invalid code for 456 input is it incorrect?
26th Jun 2019, 4:59 PM
Ankit Tiwari
Ankit Tiwari - avatar
- 1
On typing 456 it does show invalid code I think its okok Beau
26th Jun 2019, 11:37 AM
Ankit Tiwari
Ankit Tiwari - avatar
- 2
you forgot to return to 0
11th Jun 2019, 3:38 PM
Cameron Braswell
Cameron Braswell - avatar