+ 1

Zip Code Validator

One test doesn’t pass, why???? zip = input() alph = 'abcdefghijklmnopqrstuvwxyz' spec=['!', '@', '#', '

#x27;, '%', '&', '*'] for c in zip: if c in spec or c in alph: print("false") if len(zip) != 5: print("false") else: print("true”)

20th Jun 2022, 11:45 PM
Evgeny Sergeev
Evgeny Sergeev - avatar
10 Réponses
+ 1
Youre close Something like zip = input() # a string try: int(zip) # converts zip to int except: print("not a number") # returns false else: if len(zip) == 5: print("pass") # will only work if there are no exceptions to catch
21st Jun 2022, 1:30 AM
Ion Kare
Ion Kare - avatar
+ 2
Ion Kare i changed “or” in for loop to “and” and it passed all. In int: Try: If zip is ints and len(zip) ==5 : Print(“ true “ ) Except: Print (“false”) Slmething like that?
21st Jun 2022, 1:24 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
+ 2
Brian Nice one-liner. The output for boolean needs to be a string and lowercase before printing to pass the test cases. zip = input() print(str(len(zip) == 5 and zip.isdigit()).lower())
29th Sep 2022, 8:40 AM
Eugene Teh
Eugene Teh - avatar
+ 1
I see you completed all python courses.. One way to solve it is turning zip variable into a int in a try except block Another method is 1liner code The way you are doing it produces many errors
21st Jun 2022, 12:57 AM
Ion Kare
Ion Kare - avatar
+ 1
Ion Kare this worked, but feels it could be actually one liner, cleaner zip = input() try: if len(zip) == 5 and int(zip): print("true") else: print("false") except: print("false")
21st Jun 2022, 1:49 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
+ 1
Ion Kare yes here it is: Clean version: zip = input() if len(zip) == 5 and int(zip): print("true") else: print("false")
21st Jun 2022, 1:51 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
+ 1
Evgeny Sergeev if you use int(zip), then try/except is needed in case zip contains a letter and causes an error. Here is a near one-liner that eliminates need for try/except by using isnumeric() in place of int(). zip = input() print(bool(len(zip)==5 and zip.isnumeric()))
21st Jun 2022, 3:26 AM
Brian
Brian - avatar
+ 1
All is correct in there own ways but its always a con .. Its so many ways to solve this problems its good you messed around with different solutions its always good to refactor the code Evgeny Sergeev choose one that fits your needs and simple .. and Brian one liner is pretty neat
21st Jun 2022, 4:24 AM
Ion Kare
Ion Kare - avatar
0
zip = input() # a string try: int(zip) # converts zip to int except: print("not a number") # returns false else: if len(zip) == 5: print("true") # will only work if there are no exceptions to catch elif len(zip) != 5: print("false") elif len(zip) == longchar(alphabets): print("true") elif len(zip) != longchar(alphabets): print("false") elif len(zip) == 6: print("false") elif len(zip) != 6: print("true") elif len(zip) == "": print("false") elif len(zip) != "": print("true")
13th Nov 2022, 6:22 PM
Anita chaubey
Anita chaubey - avatar
- 1
zip = input() # a string try: int(zip) # converts zip to int except: print("not a number") # returns false else: if len(zip) == 5: print("true") # will only work if there are no exceptions to catch elif len(zip) != 5: print("false") elif len(zip) == longchar(alphabets): print("true") elif len(zip) != longchar(alphabets): print("false") elif len(zip) == 6: print("false") elif len(zip) != 6: print("true") elif len(zip) == "": print("false") elif len(zip) != "": print("true")
13th Nov 2022, 6:22 PM
Anita chaubey
Anita chaubey - avatar