# [Tutor] How can this code be made even better ?

Laura Creighton lac at openend.se
Thu Aug 6 12:38:38 CEST 2015

```In a message of Thu, 06 Aug 2015 11:24:21 +0545, Aadesh Shrestha writes:
>import re
>
>text = input('Enter your text with phone number using xx-xxxxxxx format \n')
>contact = re.compile(r'\d\d-\d\d\d\d\d\d\d')
>
>for i in range(len(text)):
>    chunk = text[i:i+10]
>    mo = contact.search(chunk)
>    if mo:
>        print('Phone Number Found:'+mo.group())
>
>
>Thanks
>
>************************

One of the things we know about human beings is that most people can
handle information in chunks of 7 plus or minus 2.  This means that 5
is the max for a lot of people.

see: https://en.wikipedia.org/wiki/The_Magical_Number_Seven,_Plus_or_Minus_Two

This means that when you ask human beings to type in a 7 digit phone number
is xxxxxxx you have already exceeded the cognitive capacity of many of them.
They will have a hard time producing 7 digits in a row without error.
If they are typing in their own phone numbers, they are more likely to
get it correct than if they are typing in somebody else's number, unless
it is a number they call frequently -- and this is changing.  Cell phones
have meant that we don't give out our numbers to our friends as often
as we used to -- we just send them an sms and the number miraculously
arrives as the destination.  And we phone our friends by tapping on their
names, and maybe a picture of them as well, and so do not get to know our
friends phone numbers.

Verifying a number is harder than producing it in the first place.  Looking
at a string of digits and checking that the digits there are the ones you
meant to type in the first place is fairly hard for most people in the
case of 7 digits.

Thus most people will be much happier if you let them type in phone numbers
as xx-xxx xxxx -- and since they will make fewer errors, it has that to
recommend it as well.  And some people will have their own way of remembering
numbers:

21-55 66 314

may be the way that somebody who has such a number remembers it, because
55 66  (something) is easier to remember than 556 6(something)

This is important for telephone numbers, but even more important if you
ever have to take credit card numbers in your application.  There is a
reason that credit card companies print their card numbers in groups of 4.

Which means, to improve your app, suggest to you users that they type
things in as xx-xxx xxxx but be very lenient and let them type in as
many spaces as they like.  Read the string in, strip out the spaces
and then feed it to your regexp to see that it is well formatted.

If you need to present the user with what they typed -- say they typed
xx-xxxxxxxx  (that was 8 x's after the dash, and see how that was harder
to read than xx-xxxx xxxx) stick spaces in before you resent it to them,
with whatever error message you want to give them.

Note:

x x x x x x x x

doesn't work either.  Adding more spaces does not make it better.  Indeed
most people try to read x x x x x x x x exactly as xxxxxxx -- one chunk.

Also, are you certain that all phone numbers will be in the form xx-xxx xxxx ?
In Sweden phone numbers are variable length, but I am told we are unusual
in that respect.

Laura
```