0

Why isn't working properly??

n = input() if (n[0] == 1 or 8 or 9) and (len(n) == 8): print("Valid") else: print("Invalid") Question is: You are given a number input, and need to check if it is a valid phone number. A valid phone number has exactly 8 digits and starts with 1, 8 or 9. Output "Valid" if the number is valid and "Invalid", if it is not. Sample Input 81239870 Sample Output Valid

3rd Jul 2021, 2:42 AM
Shahir
Shahir - avatar
2 Answers
+ 1
if (n[0]==1 or n[0]==8 or n[0]==9) and (len(n)==8): # or: if n[0] in (1,8,9) and len(n)==8: # but if n is a string, you must compare with strings: if n[0] in "189" and len(n)==8: # and you probably must test if string is only made of digits: n.isdigit()
3rd Jul 2021, 2:48 AM
visph
visph - avatar
+ 1
You need to use regular expressions. If necessary repeat the relevant sections of Python Core.
3rd Jul 2021, 2:49 AM
Simon Sauter
Simon Sauter - avatar