我正在制作一个程序,我已经定义了“checkpassword”部分,但是出于某种原因,我尝试了“checkpassword”选项,嵌套的选择只在某一点上起作用。这是代码中无法正常工作的部分:
Uppercase = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Lowercase = set("abcdefghijklmnopqrstuvwxyz")
Digits = set("0123456789")
AllowedSymbols = set("!$%^&*()-_=+")
if any ((c in Uppercase)for c in UserPassword):
print ("You have used at least one uppercase letter. 5 points have been awarded.")
TotalScore = TotalScore + 5
if any ((c in Lowercase)for c in UserPassword):
print ("You have used at least one lowercase letter. 5 points have been awarded.")
TotalScore = TotalScore + 5
if any ((c in Digits)for c in UserPassword):
print ("You hve used at least one digit. 5 points have been awarded.")
TotalScore = TotalScore + 5
if any ((c in AllowedSymbols)for c in UserPassword):
print ("You have used at least one of the allowed symbols. 5 points have been awarded.")
TotalScore = TotalScore + 5
if any ((c in Uppercase)for c in UserPassword) and any ((c in Lowercase)for c in UserPassword) and ((c in Digits)for c in UserPassword) and ((c in AllowedSymbols)for c in UserPassword):
print ("You have used at least one of all the allowed characters. 10 point have been awarded")
TotalScore = TotalScore + 10
else:
print (" You haven't used any of the allowed characters so no points have been awarded.")
print ("The score for your password so far is",TotalScore)
有人能告诉我哪里出了问题吗? 谢谢你
我不得不重新编写你的代码一点,因为有缩进错误和元素丢失重建你的问题。最好是把我们所需要的一切都包括进来,以重现你所看到的一切。此代码将无错误运行(Python2.7)
输出:
但是,根据您构建代码的方式,如果用户只输入“a”,则脚本返回:
You haven't used any of the allowed characters so no points have been awarded. ('The score for your password so far is', 0)
,这不是有效的返回,因为“a”是允许的字符。看看你的逻辑和缩进,看看你是否可以得到任何有效的输入,以奖励点您还可以通过使用set操作来改进这一点。以下是执行此操作的代码的固定版本:
确实要使用嵌套的if语句吗?按照此代码当前的工作方式,如果密码也有大写数字,则只检查小写数字。如果我理解你的意图,这似乎与你的逻辑相反。你现在有
你想要的是
相关问题 更多 >
编程相关推荐