Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Phone Number
#1
def check_phone():
while True:
phone = input("Please provide your phone number: ")
if len(phone)!=10 or ' ' in phone or phone.isdigit!=True:
print("No")
else:
print("Yes")

Conditions to check:10 digits, no whitespace,length has to 10.

Please provide your phone number: 9 99999999
No
Please provide your phone number: 999999999A
No
Please provide your phone number: 999999999
No
Please provide your phone number: 9999999999
No - STILL SAYS NO.
Where did I go wrong ?

Thanks
Reply
#2
You can simply check what your conditions return:

>>> phone = '9999999999' 
>>> len(phone) != 10
False
>>> '' in phone
True
>>> phone.isdigit != True 
True
It quite obvious that this should return No as at least one condition is True.

EDIT:

Some explanation as well:

- len() comparison is OK
- you probably want to check ' ' not ''. My mistake, you checked for ' ' but this is actually not necessary, see below
- you probably want phone.isdigit() not phone.isidigit

You don't need to check for space in string, str.isdigit() it does it for you under the hood:

>>> help(str.isdigit)
Help on method_descriptor:

isdigit(self, /)
    Return True if the string is a digit string, False otherwise.
    
    A string is a digit string if all characters in the string are digits and there
    is at least one character in the string.
(END)
Space is obviously not a digit, so it will return False if space is present.

In this case I would use 'positive' matching (True) not 'negative' (False):

if len(phone) == 10 and phone.isdigit():
     # this is phone number

# alternative with built-in all:

if all((len(phone) == 10, phone.isdigit())):
    # this is phone number
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
A phone number can not only contain numbers.

it may also be a country code prefixed.
Reply
#4
(Aug-22-2019, 08:10 AM)Axel_Erfurt Wrote: A phone number can not only contain numbers.

it may also be a country code prefixed.

Yep, in real life scenarious there can be spaces in phone numbers as well as hypens ('-') and country code prefix ('+'). So numbers must be 'cleaned' more thoroughly.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Microsoft text phone verifying account cito 2 983 Jul-21-2022, 12:16 PM
Last Post: cito
  Send SMS from my phone number aster 3 2,722 Jul-03-2021, 02:34 PM
Last Post: ndc85430
  lists Name Address Phone Heyjoe 2 1,783 Jun-21-2020, 10:47 AM
Last Post: Heyjoe
  Access phone via Bluetooth maxwell 2 2,282 Jun-03-2020, 05:22 PM
Last Post: maxwell
  Phone numbers ovidius80 2 2,041 Mar-12-2020, 07:08 PM
Last Post: Larz60+
  Phone app from python code Androsmeq 1 2,233 Mar-29-2019, 05:16 PM
Last Post: gruntfutuk
  Trying to move a RGB ledstrip with slider of my phone in almost "realtime" help Edris89 0 1,956 Sep-28-2018, 11:03 PM
Last Post: Edris89

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020