Python验证代码不起作用

时间:2014-10-22 22:12:55

标签: python validation if-statement while-loop

我在创建这个Python代码方面遇到了一些问题,以便验证某人输入他们的电子邮件两次,然后检查他们是否确实输入了某些内容,然后检查它们是否匹配,最后是检查它们是否都包含" @"符号。我现在还不熟悉python,我现在很困惑。

if len(email1) == 0 or len(email2) == 0:
    if email1 != email2:
        while "@" not in email1 or "@" not in email2:
            print "Your email is not valid!"
            email1 = raw_input("\nPlease enter your email address")
            time.sleep(1)
            email2 = raw_input("\nPlease re-enter your email address")
            delay_print("Validating Email......")
        else:
            print "Email accepted!"
    else:
        pass
else:
    f.write(email1)

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

也许你想尝试像

这样的东西
while not email1 or not email1 == email2 or not '@' in email1:

答案 1 :(得分:0)

为什么要使用regex

import re
import time
regex=re.compile("[^@]+@[^@]+\.[^@]+")

validMail=False
while not validMail:
    email1 = raw_input("\nPlease enter your email address ")
    email2 = raw_input("\nPlease re-enter your email address ")

    if len(email1)>0 and len(email2) > 0:
        if regex.search(email1) and regex.search(email2):
            if email1 == email2:
                print "Email accepted!"
                validMail=True
            else:
                print "different mail"
        else:
            print "Your email is not valid!"
    else:
        print "empty email"