0

Solve using basic conditions of python

Phone Number Validator 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

13th Sep 2022, 1:14 PM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
9 odpowiedzi
+ 2
number = int(input()) if len(str(number))==8: if (str(number)[0]=="1" or str(number)[0]=="8" or str(number)[0]=="9"): print("Valid") else: print("Invalid") else: print("Invalid") #I have tried this out and this works;)
17th Sep 2022, 1:39 PM
Diksha Sharma
Diksha Sharma - avatar
+ 2
And Shahram's code is working buddy
18th Sep 2022, 10:52 AM
Diksha Sharma
Diksha Sharma - avatar
+ 1
Use if else condition to check if length of the string is equal to 8 and check if string[0]==1 or 8 or 9 If all conditions return true print Valid else print Invalid.
13th Sep 2022, 1:41 PM
Shahram
Shahram - avatar
+ 1
Please send the code
13th Sep 2022, 7:07 PM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
+ 1
phone = input() if (len(phone)==8) and (phone[0]=="1" or phone[0]=="8" or phone[0]=="9"): print('Valid') else: print('Invalid')
13th Sep 2022, 7:09 PM
Shahram
Shahram - avatar
+ 1
num = input ("Enter number: ") if len(num) > 8: print("Invalid") elif num[0] != 1 or num[0] != 8 or num[0] != 9: print("Invalid") else: print("Valid") 😭😭😭 I know it's long but don't worry about me, I'll be fine
14th Sep 2022, 8:05 AM
Stephen Ayodele
Stephen Ayodele - avatar
+ 1
Hey it's not working for 81234567
14th Sep 2022, 5:29 PM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
+ 1
Why the rest of code are not working except yours... What's the problem in there code ?
18th Sep 2022, 7:16 AM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
+ 1
In Stephen's code, you can see in the elif statement that he compared the not equal value with integers, whereas he has taken an input of string, "1","8","9"..it should be like this.
18th Sep 2022, 10:49 AM
Diksha Sharma
Diksha Sharma - avatar