Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9
「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
Lothar - 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
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
+ 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
5Just - 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
Rain - avatar