2017-11-25 22 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