2022年 11月 7日

Python实现邮箱合法性校验(中软国际机试)

目录

题目描述

输入示例

输出示例

题目分析

代码

传送门


题目描述

输入一个电子邮箱地址字符串,要求检查这个邮箱地址是否合法。如果输入的电子邮箱地址是合法的,输出字符串1,否则输出字符串0。满足如下条件被认为是合法的邮箱地址:
    1、仅包含一个’@’字符
    2、最后三个字符必须是’.com’
    3、字符之间没有空格
    4、有效字符为 0-9、大小写字母、’.’、’@’、’_’

输入示例

huawei@chinasofti.com

输出示例

1

题目分析

根据题目列出的合法性规则,逐一检查输入的字符串是否满足合法的邮箱地址。

代码

  1. def check_email_url(email_address):
  2. # check '@'
  3. at_count = 0
  4. for element in email_address:
  5. if element == '@':
  6. at_count = at_count + 1
  7. if at_count != 1:
  8. return 0
  9. # check ' '
  10. for element in email_address:
  11. if element == ' ':
  12. return 0
  13. # check '.com'
  14. postfix = email_address[-4:]
  15. if postfix != '.com':
  16. return 0
  17. # check char
  18. for element in email_address:
  19. if element.isalpha() == False and element.isdigit() == False:
  20. if element != '.' and element != '@' and element != '_':
  21. return 0
  22. return 1
  23. # main
  24. email = input()
  25. print(check_email_url(email))

传送门

1.  input()函数

https://blog.csdn.net/TCatTime/article/details/82556033

2.  isalpha()函数

Python str isalpha方法_TCatTime的博客-CSDN博客_python str.isalpha

3.  isdigit()函数

Python str isdigit方法_TCatTime的博客-CSDN博客_str.isdigit