【问题标题】:Need to check if there is string in front of the @ symbol需要检查@符号前面是否有字符串
【发布时间】:2017-03-12 19:47:56
【问题描述】:

编写一些代码来检查是否以正确的方式输入了电子邮件,我有所有的代码来检查是@符号和。都在字符串中,以及整个电子邮件有一定的长度,但我似乎无法整理一些代码来检查@符号之前是否有任何字符串,现在如果你甚至输入@gmail.com,它就会出现正确。

email = input ("Enter an email address for you account: ")
check2 = 0

if "@" in email:
    check2 = (check2)+1
else:
    check2 = (check2)

if "." in email:
    check2 = (check2)+1
else:
    check2 = (check2)

if len(email)>6:
    check2 = (check2)+1
else:
    check2 = (check2)


if check2==3:
    print ("Your email was accepted")
else:
    print ("Error")

【问题讨论】:

    标签: python validation email input


    【解决方案1】:

    添加

    if email[0] == '@':
        check2 = check2
    else:
        check2 += 1
    

    并将最后一个 if/else 更新为

    if check2 == 4:
        print ("Your email was accepted")
    else:
        print ("Error")
    

    【讨论】:

    • 最好先剥离() 输入字符串以避免@ 之前只有空格。像这样email = strip(email)
    • 非常感谢
    【解决方案2】:

    您可以检查输入的电子邮件地址中'@'的索引是否为0。如果为0,则表示第一个字母是'@'。下面是代码:

    str = "@gmail.com"
    
    if(str.index('@')==0):
        print("INVALID")
    

    【讨论】:

    • 也谢谢你,非常感谢你的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 2021-06-24
    • 1970-01-01
    • 2022-06-25
    • 2019-06-06
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多