is there an easier way to write this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

is there an easier way to write this code?

x= input() count=0 for i in x: if i.isalpha() or len(x)!=5: count+=1 if count==0 : print("true") else: print("false") #i actually wrote this code for zip code validator and i was wondering how can i code it in an easier way cuz i think this is too complicated for it. at least thats how i think :))

19th Mar 2023, 1:19 PM
behdad sharifian
behdad sharifian - avatar
2 Answers
+ 7
You don't need to use a loop here. Simply check len( x ) == 5 and x.isdigit() yields true. Print 'true' when both conditions are satisfied, 'false' otherwise ...
19th Mar 2023, 1:32 PM
Ipang
+ 2
tnx alot :D
19th Mar 2023, 2:14 PM
behdad sharifian
behdad sharifian - avatar