Python first, the first applet, judge the phone number, and the python Applet

Source: Internet
Author: User

Python first, the first applet, judge the phone number, and the python Applet

  • General steps for coding
    • Define Requirements
    • Understanding requirements
    • Summarize the problem
    • Write code
    • Test and debug code
  • Requirement
    • Enter a string 'Call me at 415-555-1011 tomorrow. 415-555-9999 is my office .'
    • Extract the phone number (415-555-1011,415-555-9999)
  • Determine the format of the phone number. Consider the rule that only numbers and '-' are allowed, and the third and seventh digits must be '-', in this case, data-driven writing is considered;
  • The Code is as follows:
    1 # coding: UTF-8 2 #__ author _ = 'diva '3 4 PHONE_NUMBER_SIZE = 12 # define the global variable. The length of the phone number is 5 6 def is_digital (ch ): # determine if it is a number 7 if ord (ch)> = ord ('0') and ord (ch) <= ord ('9 '): 8 return True 9 return False10 11 def is_special (ch): # determine whether it is '-'12 if ord (ch) = ord ('-'): 13 return True14 return False15 16 def is_phone_number_check (chars): # judge the format 17 if len (chars )! = PHONE_NUMBER_SIZE: 18 return False19 20 for I in range (PHONE_NUMBER_SIZE): 21 if not is_digital (chars [I]) and not is_special (chars [I]): # exit 22 instead of a number or '-' return False23 if (I = 3 or I = 7) and not is_special (chars [I]): # exit 24 return False25 if (I! = 3 and I! = 7) and not is_digital (chars [I]): # exclude the third and seventh digits. If it is not a number, exit 26 return False27 return True28 29 def split_phone_number (message): # filter out the number, 30 for n in range (len (message): 31 chars = message [n: n + PHONE_NUMBER_SIZE] 32 if is_phone_number_check (chars ): 33 print ('found phone number: '+ chars) 34 print ('done. ') 35 36 if _ name _ =' _ main _ ': 37 message = 'Call me at 415-555-1011 tomorrow. 415-555-9999 is my office. '38 split_phone_number (message)

    # It is best that you do not need to do anything. You just need to pass the parameter in. The function originally has the function of hiding the implementation: you only need to pass the message in the main function to know the result.
  • Test Results
  •  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.