0

Zip Code Validator

I need help with finding the solutions... My test is failing on two tests in the challenge...

25th Mar 2024, 4:56 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
17 Réponses
+ 6
You can use the isdigit function to find out if it is a digit so that it returns the Value True if it is true, and see if the size of the zip code is equal to the size of the zip code which is 5 code = int(input()) if code.isdigit() and len(code)==5: print("true") else: print("false") I provide you with the code so you can understand how it works and you can go deeper into it so you can use what you have learned.
25th Mar 2024, 5:09 AM
STE4LPH
STE4LPH - avatar
+ 6
Wait a moment, you are using the re library, you should apply what you have learned with the re library, since it is very useful, you should do it like this What pattern does is the following \d makes me look for if any number has been typed and {5} is a size of 5 digits at the end $ import re pattern=r'\d{5}
#x27; if re.match(pattern,input()): print('true') else: print('false') If you are correct, mark my answer. In conclusion, learn more about the re library since it is very useful on many occasions. I recommend it to everyone since it will be useful to you later.
25th Mar 2024, 5:33 AM
STE4LPH
STE4LPH - avatar
+ 5
^\d{5}$ ^ -> matches the beginning of the string \d{5} -> Matches exactly 5 digits $ -> if match at end of string
25th Mar 2024, 5:58 AM
STE4LPH
STE4LPH - avatar
+ 4
Used to finish a pattern For example: \d{5}$ The $ would serve as this whole pattern \de{5} has to be fulfilled If you don't understand, let me know.
25th Mar 2024, 5:42 AM
STE4LPH
STE4LPH - avatar
+ 4
You imported re but it is not used. However, it is OK because we can do it without re. In this statement, int_version = int(zip_code), what will happen if the zip_code is "a1234"?
25th Mar 2024, 5:45 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 4
It will send False since as you will see \d serves to identify that they are digits and not letters complete the python core course there it will teach you how to use the library I recommend it https://www.sololearn.com/learn/courses/le-python
25th Mar 2024, 5:50 AM
STE4LPH
STE4LPH - avatar
+ 3
If you want to know why your code doesn't work, you have to post your code so others can inspect it.
25th Mar 2024, 5:13 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Ntsikelelo Nkosingiphile Kevin Khoza Surely"a1234" is not a valid zip code and should return "false" as output. But your code cannot handle that input and produce a runtime error. https://sololearn.com/compiler-playground/ccZO818Td4GC/?ref=app
25th Mar 2024, 5:57 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Ntsikelelo Nkosingiphile Kevin Khoza You can't pass a test case if your code: produce a result which is not exactly as expected; your code terminated with error. That's why it failed the tests. I see you already pass the intermediate course, and also the legacy Python courses. I think you already know try...except block. Further examining your code, I found variable "max_length" is not used. What is the purpose of it? Also, it can be solved without using any module or string method. You can use a for loop to check each character and see if it is in a string which composite of numbers only.
25th Mar 2024, 6:15 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Wong Hei Ming My sotulion is currently like this: zip_code = input() import re max_length = 5 int_version = int(zip_code) if len(zip_code) == 5 and type(int_version) is int: print("true") else: print("false") It fails the third, and fourth tests...
25th Mar 2024, 5:27 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
+ 1
Wong Hei Ming I understand that is raising exceptionz.. So it is probably the reason why my code didn't work? It was failing on alphanumeric strings
25th Mar 2024, 6:03 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
0
STE4LPH thank you for the solution... 🙏🏿🙏🏿 I will try it out...
25th Mar 2024, 5:24 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
0
STE4LPH so what does the $ sign do... I think I saw them say "r" is a standard for showing that we're working with a regEX pattern, please correct me if I'm wrong. But what does the "
quot; sign do?
25th Mar 2024, 5:38 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
0
I think that provides enough clearity, with how concise it is... And thank you once again the solution works
25th Mar 2024, 5:43 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
0
Wong Hei Ming the requirements say it must be only numbers, contain no spaces and special characters... So that one is a false condition
25th Mar 2024, 5:53 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
0
Wong Hei Ming I will go back and check out the course. Because it has been a while.
25th Mar 2024, 7:09 AM
Ntsikelelo Nkosingiphile Kevin Khoza
Ntsikelelo Nkosingiphile Kevin Khoza - avatar
0
My solution is pretty simple and uses no library: https://sololearn.com/compiler-playground/cPyra4V25KzY/?ref=app
26th Mar 2024, 8:15 AM
Shreyash Roopnah
Shreyash Roopnah - avatar