我的嵌套选择无法正常工作。Python

2024-09-27 07:28:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个程序,我已经定义了“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)

有人能告诉我哪里出了问题吗? 谢谢你


Tags: inyouforifhaveanyoneat
3条回答

我不得不重新编写你的代码一点,因为有缩进错误和元素丢失重建你的问题。最好是把我们所需要的一切都包括进来,以重现你所看到的一切。此代码将无错误运行(Python2.7)

TotalScore = 0
UserPassword = raw_input("> ")

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)

输出:

>Aa0$
You have used at least one uppercase letter. 5 points have been awarded.
You have used at least one lowercase letter. 5 points have been awarded.
You hve used at least one digit. 5 points have been awarded.
You have used at least one of the allowed symbols. 5 points have been awarded.
You have used at least one of all the allowed characters. 10 point have been awarded
('The score for your password so far is', 30)

但是,根据您构建代码的方式,如果用户只输入“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操作来改进这一点。以下是执行此操作的代码的固定版本:

import string

TotalScore = 0
passwd = set(UserPassword) # create set from UserPassword
Uppercase = set(string.ascii_uppercase)
Lowercase = set(string.ascii_lowercase)
Digits = set(string.digits)
AllowedSymbols = set("!$%^&*()-_=+")

if passwd.intersection(Uppercase):
    print ("You have used at least one uppercase letter. 5 points have been awarded.")
    TotalScore += 5
if passwd.intersection(Lowercase):
    print ("You have used at least one lowercase letter. 5 points have been awarded.")
    TotalScore += 5
if passwd.intersection(Digits):
    print ("You have used at least one digit. 5 points have been awarded.")
    TotalScore += 5
if passwd.intersection(AllowedSymbols):
    print ("You have used at least one of the allowed symbols. 5 points have been awarded.")
    TotalScore += 5
if passwd.intersection(Uppercase.union(Lowercase, Digits, AllowedSymbols)):
    print ("You have used at least one of all the allowed characters. 10 point have been awarded")
    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)

确实要使用嵌套的if语句吗?按照此代码当前的工作方式,如果密码也有大写数字,则只检查小写数字。如果我理解你的意图,这似乎与你的逻辑相反。你现在有

if (password satisfies some condition) add points,
   if (password ALSO satisfies some other condition) add points

你想要的是

if (password satisfies some condition) add points
if (password satisfies some other condition) add points

相关问题 更多 >

    热门问题