我的elif / if语句不起作用

时间:2015-12-12 16:23:10

标签: python

choice=str("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")
choice1=str("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z")
choice2=(0,1,2,3,4,5,6,7,8,9)
user=int(len(input("Enter password")))
while user <6 and user <12:
    user=int(len(input("The password needs to be at least 6 characters and no more than 12, enter password again")))
print("password accepted")

if user in (choice2) or   str(choice) or  (choice1):
    print("password is weak")
elif user in  (choice2) and  str(choice1) or  (choice) and (choice) or  (choice1) and (choice2):
    print("your password is medium")
elif user in (choice2) and  (choice1) and  (choice):
    print("your password is strong")

当用户输入密码时:

Enter passwordJnnnnn
password accepted
password is weak

它仍然回复他们的密码很弱,当它假设他们的密码是中等

当我添加这个:

if user in (choice2) or user in str(choice) or user in (choice1):
    print("password is weak")
elif user in  (choice2) and user in   str(choice1) or  user in (choice) and  (choice) or user in   (choice1) and  (choice2):
    print("your password is medium")
elif user in (choice2) and user in  str(choice1) and user in   (choice):
    print("your password is strong")

它仍然不起作用:

Enter passwordLhgg12
password is weak
当它假设强大时,它会回复弱势

4 个答案:

答案 0 :(得分:0)

您需要将条件更改为:

if user in (choice2) or user in str(choice) or user in (choice1):

和其他条件一样。

那是因为python会将str(choice)评估为True,并且第一个条件始终为True。

阅读Truth Value Checking中的详细信息。

答案 1 :(得分:0)

您需要在每个和/或

之间使用布尔表达式

您使用

评估了您的while循环
while user <6 and user <12:

你的if语句在条件中没有in,所以我想这个

if user in (choice2) or   str(choice) or  (choice1):

你的意思是

if user in (choice2) or user in str(choice) or user in  (choice1):

其他精灵也一样

答案 2 :(得分:0)

我发现您的代码有几个问题:

choice=str("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z")

你可能想要的是所有角色,不包括',' 25次,所以

choice = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"  # no need for str(), it is already a str

然后,choice2是一个整数元组...我想你又想要角色了:

choice2 ="0123456789"

在这里您要求输入密码,忘记密码并在名为user的变量中存储密码长度(顺便说一下,int(len(X))len(X)相同):

user=int(len(input("Enter password")))

在这里检查用户是否小于6且小于12(当然,如果&lt; 6,则&#39; s&lt; 12)

while user <6 and user <12:
    ...

我猜你真的想要:

password = input("Enter password")
while not (6 <= len(password) <= 12):
    ...

这是您的主要问题

if user in (choice2) or   str(choice) or  (choice1):
    print("password is weak")

检查其中任何一个是否为真:

  • 用户(整数,密码长度)在选择2(数字0-9)中,即用户&lt; 10
  • str(choice)不是空字符串
  • choice1不是假(0,无,&#34;&#34;,[],False都是假的)

这将永远过去!

我想在你的测试中你想检查密码是否包括来自选择的字符(和choice1和choice2)。为此,你可以这样做:

any(ch in choice1 for ch in password)  # is any character from password in choice1?

set(password) & set(choice1)  # is there any overlap between contents of the two?

答案 3 :(得分:0)

除了其他人所说的比较运算符不是分布式的,userint,其中choicechoice1是字符串。如果您尝试bool(user in choice1),我相信您会收到错误。

您可以将输入保存为与其长度不同的变量,还可以创建一个集来进行比较:

password = raw_input("Input password:\n>>")
pass_length = len(password)
pass_chars = set(password)

然后您的choice类别也需要设置,但您可以像现有的那样编写if/elif

if pass_chars in choice or pass_chars in choice1:

...等