2017-11-25 149 views
-3

所以我没有收到任何错误,但它没有正确运行。用# **突出显示的代码位是错误的地方。因此,当我运行它时,无论是否放入.com或.co.uk或.org.uk,它总是会打印出我的代码无效。程序无法正常工作

import time 
#Imports the time library. 

#3 
#Email validator 
#Make a program to check whether an email address is valid or not. 
#You could make sure that there are no spaces, that there is an @ symbol and a dot somewhere after it. Also check that the end parts of the address are not blank. 
#Extensions: 
#1. When an email address is found to be invalid, tell the user exactly what they did wrong with their email address rather than just saying it is invalid 
#2. Allow the user to choose to give a text file with a list of email addresses and have it process them all automatically. 


print("Only .com or .co.uk or .org.uk are accepted.") 
def ev(): 
    #starts the definition and defines the command. 
    time.sleep(1) 
    #1 second wait 
    email = input("Email: ") 
    dot = "." 
    at = "@" 
    space = " " 
    com = ".com" 
    couk = ".co.uk" 
    org = ".org.uk" 
    if at not in email: 
     print("Invalid. There is no @ in your email.") 
     #Says email is invalid as there is no @ 
     ev() 
    elif dot not in email: 
     print("Invalid. There is no .(dot) in your email.") 
     #Says email is invalid as there is no . 
     ev() 
     #Loops to asking for the email again 
    elif space in email: 
     print("Invalid. There shouldn't be any spaces in your email.") 
     #Says email is invalid as there is a space 
     ev() 
     #Loops to asking for the email again 
    elif com not in email or couk not in email or org not in email: # ** 
     print("This email is not valid. Only .com or .co.uk or .org.uk are accepted.")** 
     ev() 
     #Loops to asking for the email again 
    else: 
     print("Valid!") 

ev() 
#Ends the definition so it starts automatically. 
+2

请[编辑]你的问题,并在错误信息添加完整的或更好,但整个回溯。只是说“不工作”和“我的代码无效”太含糊。 – martineau

+0

答案可能已经解决了您最初的担忧,但使用[正则表达式]会更容易(并且与当前代码不同)(https://docs.python.org/3/library/) re.html) –

+0

你有一个答案,但我们还要指出,正则表达式会更有效。另外,将它作为一个验证函数来组织它是一件好事,该函数接受一个字符串并返回一条错误消息 - 您可以轻松地为它编写单元测试 - 以及一个单独的函数,它读取输入,调用validitor,打印结果,并循环。 – Jerry101

回答

3

你需要and,而不是or

试试这个:

elif com not in email and couk not in email and org not in email: 
    print("This email is not valid. Only .com or .co.uk or .org.uk are accepted.")** 
    ev() 
    #Loops to asking for the email again 
+1

Thnx!现在完美的工作! – wacraby