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

题目描述

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

输入示例

[email protected]

输出示例

1

题目分析

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

代码

def check_email_url(email_address):
    # check '@'
    at_count = 0
    for element in email_address:
        if element == '@':
            at_count = at_count + 1

    if at_count != 1:
        return 0

    # check ' '
    for element in email_address:
        if element == ' ':
            return 0

    # check '.com'
    postfix = email_address[-4:]
    if postfix != '.com':
        return 0

    # check char
    for element in email_address:
        if element.isalpha() == False and element.isdigit() == False:
            if element != '.' and element != '@' and element != '_':
                return 0

    return 1

# main
email = input()
print(check_email_url(email))

传送门

1.  input()函数

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

2.  isalpha()函数

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

3.  isdigit()函数

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

猜你喜欢

转载自blog.csdn.net/TCatTime/article/details/82726307
今日推荐