Python 程序是';t工作不正常
因此,我没有得到任何错误,但它没有正确运行。用Python 程序是';t工作不正常,python,python-3.x,Python,Python 3.x,因此,我没有得到任何错误,但它没有正确运行。用#**突出显示的代码位就是出错的地方。因此,当我运行它时,无论我是否输入.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
#**
突出显示的代码位就是出错的地方。因此,当我运行它时,无论我是否输入.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.
您需要
和
而不是或
:
试试这个:
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
请将您的问题逐字逐句地添加到错误消息中,或者更好地添加整个回溯。仅仅说“不起作用”和“我的代码无效”都太模糊了。答案可能已经解决了您最初的顾虑,但如果您有答案,使用它会更容易(并且可以防止错误,与当前代码不同),但我们也要指出,正则表达式会更有效。另外,最好将其组织为一个接受字符串并返回错误消息的验证器函数——您可以很容易地为此编写单元测试——以及一个单独的函数,该函数读取输入、调用validitor、打印结果,并为adviceThnx循环.thnx!现在效果很好!