Zip code validator python question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Zip code validator python question

Looking for some explanation to help me understand why my code doesn't work for the "zip code validator" code coach. I pass solutions 2 through 5, but not 1 and 6. I'm stumped as to why. entry = input() if (type(entry) is int) and (len(entry) == 5) and (entry.count(" ") == 0): print("true") else: print("false")

27th Jun 2023, 2:36 AM
the-napster
5 Answers
+ 3
your statement " type( entry) is int " always returns false as input is taken as string type...
27th Jun 2023, 3:48 AM
Jayakrishna 🇮🇳
+ 3
Ah of course inputs are str type. Thank you all for your help
27th Jun 2023, 4:52 PM
the-napster
+ 2
Jayakrishna 🇮🇳 Yes, indeed it is. the-napster Try using try/except block with entry = int(input()), or try using list comprehension like this:(Just a little example) n = [x for x if x.isalpha() or not x.isalnum()] print("false" if len(n) >= 1 or len(entry)!=5 else "true") In the following code, we use list comprehension to collect all characters that are not numbers. `isalpha()` returns True if the character is in alphabet, `isalnum()` returns True if the character is alphabet numerical(a-zA-Z0-9). Here, we used the "not" operator for `isalnum()` to gather all symbols of the string.. Next, we used the len () function to get the length of n and check if the length of it is greater/equal to 1 or not.. Lastly, we used ... if condition else ... to reduce code line.
27th Jun 2023, 4:11 AM
Dragon RB
Dragon RB - avatar
+ 2
the-napster now that you have Jayakrishna 🇮🇳's answer, you will need to find a better way to test for numeric input. Python provides a useful string object method, isnumeric(), that returns True if all characters are numeric, else False if any are non-numeric. Try: if entry.isnumeric() and len(entry)==5:
27th Jun 2023, 4:54 AM
Brian
Brian - avatar
+ 1
Try solving this problem with try/except block!
27th Jun 2023, 4:11 AM
Dragon RB
Dragon RB - avatar