+ 2

different solutions for the "zip code validator" challenge

Hi everybody, I solved the "zip code validator" in code coach with this code: code = input() if (len(code) == 5) and (code.count(" ") == 0) and (int(code)/1 == int(code)): print("true") else: print("false") --- I think there are more nice solutions possible - what are your ideas? Here is the task: As headmaster of the post office, sometimes people write zip codes that don't exist or zip codes that are not valid. You are tasked with making a system to check if the inputted zip code is a valid zip code. Task: Write a program that takes in a string representing a zip code. Output true or false if it is a valid zip code or not. A valid zip code is only numbers, must be 5 characters in length, and contain no spaces. Input Format: A string containing a zip code. Output Format: A string: true is the input is a valid zip code, or false, if it is not. Sample Input: 752f78 Sample Output: false

5th Apr 2022, 12:12 PM
David Lau
David Lau - avatar
8 Answers
+ 5
You may try something like this if code.isdigit() and len(code)==5: print("true") else: print("false")
5th Apr 2022, 1:36 PM
Simba
Simba - avatar
+ 3
import re pattern=r'\d{5}
#x27; if re.match(pattern,input()): print('true') else: print('false') # Course: Python Core # Regular Expressions
11th May 2022, 9:55 PM
MING ZI CHEN
MING ZI CHEN - avatar
0
import re print(* ['true' if re.findall(r'^\d{5}
#x27;, input()) else 'false'])
21st Sep 2022, 12:49 PM
Jacopo Cossu
Jacopo Cossu - avatar
0
hey sir, your solve has a bug please pay attention to it. yours sincerely.
24th Dec 2022, 3:09 PM
Yaser Hashemi
Yaser Hashemi - avatar
0
code=input() if code.isdigit() and len(code)==5: print("true") else: print("false") Try some unique code Definitely it will work
28th Dec 2022, 1:32 PM
Laxmi Mehta
Laxmi Mehta - avatar
0
is your len(code)==5 working? because mine is not
13th Feb 2023, 9:04 PM
Farah Baliki
Farah Baliki - avatar
0
zip_code = input() if len(zip_code) == 5 and (zip_code.count(" ") == 0) and (int(zip_code)/1 == int(zip_code)): print("true") else: print("false") This solution passed every test cases.
10th Mar 2023, 8:10 AM
JANHVI KOLHE
JANHVI KOLHE - avatar
0
try: code = int(input()) if len(str(code)) == 5: print("true") else: print("false") except ValueError: print("false") This is my solution
2nd Jul 2024, 5:49 AM
Solomon Adenuga