+ 1
Zip Code Validator
One test doesn’t pass, why???? zip = input() alph = 'abcdefghijklmnopqrstuvwxyz' spec=['!', '@', '#', '
#x27;, '%', '&', '*'] for c in zip: if c in spec or c in alph: print("false") if len(zip) != 5: print("false") else: print("true”)10 Antworten
+ 1
Youre close
Something like
zip = input() # a string
try:
int(zip) # converts zip to int
except:
print("not a number") # returns false
else:
if len(zip) == 5:
print("pass") # will only work if there are no exceptions to catch
+ 2
Ion Kare i changed “or” in for loop to “and” and it passed all.
In int:
Try:
If zip is ints and len(zip) ==5 :
Print(“ true “ )
Except:
Print (“false”)
Slmething like that?
+ 2
Brian Nice one-liner. The output for boolean needs to be a string and lowercase before printing to pass the test cases.
zip = input()
print(str(len(zip) == 5 and zip.isdigit()).lower())
+ 1
I see you completed all python courses..
One way to solve it is turning zip variable into a int in a try except block
Another method is 1liner code
The way you are doing it produces many errors
+ 1
Ion Kare this worked, but feels it could be actually one liner, cleaner
zip = input()
try:
if len(zip) == 5 and int(zip):
print("true")
else:
print("false")
except:
print("false")
+ 1
Ion Kare yes here it is:
Clean version:
zip = input()
if len(zip) == 5 and int(zip):
print("true")
else:
print("false")
+ 1
Evgeny Sergeev if you use int(zip), then try/except is needed in case zip contains a letter and causes an error.
Here is a near one-liner that eliminates need for try/except by using isnumeric() in place of int().
zip = input()
print(bool(len(zip)==5 and zip.isnumeric()))
+ 1
All is correct in there own ways but its always a con ..
Its so many ways to solve this problems its good you messed around with different solutions its always good to refactor the code Evgeny Sergeev choose one that fits your needs and simple ..
and Brian one liner is pretty neat
0
zip = input() # a string
try: int(zip) # converts zip to int
except:
print("not a number") # returns false
else:
if len(zip) == 5:
print("true")
# will only work if there are no exceptions to catch
elif len(zip) != 5:
print("false")
elif len(zip) == longchar(alphabets):
print("true")
elif len(zip) != longchar(alphabets):
print("false")
elif len(zip) == 6:
print("false")
elif len(zip) != 6:
print("true")
elif len(zip) == "":
print("false")
elif len(zip) != "":
print("true")
- 1
zip = input() # a string
try: int(zip) # converts zip to int
except:
print("not a number") # returns false
else:
if len(zip) == 5:
print("true")
# will only work if there are no exceptions to catch
elif len(zip) != 5:
print("false")
elif len(zip) == longchar(alphabets):
print("true")
elif len(zip) != longchar(alphabets):
print("false")
elif len(zip) == 6:
print("false")
elif len(zip) != 6:
print("true")
elif len(zip) == "":
print("false")
elif len(zip) != "":
print("true")