我的嵌套选择不能正常工作。蟒蛇

时间:2017-11-04 15:42:21

标签: python

我正在制作一个程序而且我已经定义了"检查密码"部分但由于某种原因我尝试了#34;检查密码"选项和嵌套选择仅适用于某一点。这是代码中无法正常工作的部分:

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)

有人可以告诉我哪里出错了吗? 谢谢。

3 个答案:

答案 0 :(得分:0)

我不得不重新编写代码,因为存在缩进错误,并且缺少重新创建问题的元素。最好包含我们需要的所有内容来重现您所看到的内容。此代码将无错误地运行(Python 2.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'是允许的字符。看看你的逻辑和缩进,看看你是否可以获得任何有效的输入来获得分数

答案 1 :(得分:0)

您确定要使用嵌套的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

答案 2 :(得分:0)

您还可以使用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)