+ 1

Is my code a good solution for the Zip Code Validator Code-Coach?

What do you think about my code? Pls tell me your thoughts. Maybe you can give me some tips or a easier way for solving this Code-Coach in Python. https://sololearn.com/compiler-playground/cxszaQAZoV3R/?ref=app

10th Dec 2023, 8:35 PM
Rene - avatar
8 Answers
+ 10
「HAPPY TO HELP」 , you may be right in recommending the use of regex, but this needs to know how to use this technology. but as far as i know, there is no python tutorial from sololearn that is teaching this in an appropriate manner. Rene , to keep it simple for beginners, we can use also: zip = input() if zip.isdigit() and len(zip) == 5: # (1) print('true') else: print ('false') (1) this line uses the string method .isdigit() and the built-in function len() > .isdigit() returns `True` if *all* characters of the string are *digits* > len() gets the *total number of characters* in the string > only if *both* conditions are `True`, the zip code is a valid code.
11th Dec 2023, 7:15 AM
Lothar [currently less active]
Lothar [currently less active] - avatar
+ 5
The code is not bad. Actually you can put the numbers into s string instead of a list. They work the same. And HAPPY TO HELP was right, regular expression is a better choice. You can learn more at about it under community section, if they haven't got removed from recent update.
11th Dec 2023, 1:25 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 5
Your solution is fine. You could make it more compact like this: digits = "0123456789" valid = len(code) == 5 and all(c in digits for c in code) print("true" if valid else "false") There are some techniques in this you may still be unfamiliar with, after the Intro to Python course. The valid variable is a Boolean (logical) type, True or False. The all() function is true when all of its arguments are true. I am using a generator expression inside, to loop all the values and check each character individually. But Lothar's suggestion to use isdigit() or isnumeric() methods of the string, has the exact same effect. In the print statement I used a conditional expression, also known as ternary expression. Regular expressions are also great but in this specific case they are really 'overkill' when it can be easily covered by built in methods. And the regex usually comes with penalty to performance, so if you have to process large amount of data, you can feel the pain :) Not in this case though.
12th Dec 2023, 3:12 AM
Tibor Santa
Tibor Santa - avatar
+ 4
With string validation, regular expressions are usually the "go to" solutions. IDK Python much but this is what I came up with; could likely be shorter. import re # Regex library # We want a string of number characters [0-9] with a length of exactly 5 {5} # nothing before it ^ and nothing after $ if re.search("^[0-9]{5}
quot;, code) == None: print( "Invalid" ) else: print("valid") Performance is debatable whether to use regex or just string operations if that matters. String manipulation straight from memory would be faster and more efficient than importing a library and parsing the regex, etc., but typing regex is faster and cleaner at least.
10th Dec 2023, 10:58 PM
「HAPPY TO HELP」 - avatar
+ 2
Thank you for your answers :D
11th Dec 2023, 12:16 PM
Rene - avatar
+ 2
You could use for i in code: if type(i)==int and len(something here)==5: print('True') else: But only if you converted the strings to int's . Just a thought, though. Your code looks good!
12th Dec 2023, 1:42 PM
5Just - avatar
+ 1
Tibor Santa Thank you for your feedback and tipps :)
12th Dec 2023, 7:24 AM
Rene - avatar
+ 1
Rene , Lots of suggestions already, so I'll just give you a bug report. This input, zcode produces this output. false false false false false true OK, one suggestion. Code like this, for i in code: if i in digits: continue else: print("false") can sometimes be tightened up by testing for the negative instead of the positive. for i in code: if i not in digits: print("false") break You save a line and also ensure that the loop stops after the first false instead of continuing to test the rest of the cases.
13th Dec 2023, 2:11 AM
Rain - avatar