我无法阻止它重复自己我不知道我哪里出错了,我已经问过如何正确循环但是我找不到如何。
pwok = 0
print ("You password must contain lower case and uppcase characters")
print ("It should also contain numbers as well")
pw = input("Please enter a password to test: ")
while pwok != 1:
caps = sum(1 for c in pw if c.isupper())
lower = sum(1 for c in pw if c.islower())
nums = sum(1 for c in pw if c.isnumeric())
scr = ['weak', 'medium', 'strong']
r = [caps, lower, nums]
if len(pw) < 6:
print("This password is too short please Re-Enter another password")
elif len(pw) > 12:
print("This password is too long please Re-Enter another password")
else:
pwok = 1
if caps >= 1:
if lower >= 1:
if nums >= 1:
print(scr[1])
elif nums < 1:
print("your password is " + scr[1])
elif lower < 1:
print("your password strength is " + scr[1])
elif caps < 1:
print("your password strength is " + scr[1])
if caps < 1:
if lower < 1:
if nums <1:
print(scr[3])
答案 0 :(得分:2)
问题是您再也没有读过密码。因此,如果密码第一次失败,您将永远陷入while循环。您应该在while循环中读取密码:
while pwok != 1:
pw = input("Please enter a password to test: ")
...
答案 1 :(得分:0)
您需要在循环内移动输入。如果len太短或太长,请在检查密码len后使用continue返回循环的开头。
如果不在循环中使用继续和移动输入,如果密码太长或太短,它将永远循环,所有行都将执行if the len pw < 6 or > 12
。
pwok = 0
print ("You password must contain lower case and uppcase characters")
print ("It should also contain numbers as well")
while True:
pw = input("Please enter a password to test: ")
if pwok ==1:
break
caps = sum(1 for c in pw if c.isupper())
lower = sum(1 for c in pw if c.islower())
nums = sum(1 for c in pw if c.isnumeric())
scr = ['weak', 'medium', 'strong']
r = [caps, lower, nums]
if len(pw) < 6:
print("This password is too short please Re-Enter another password")
continue # if you don't use continue all lines will be executed even if len < 6
elif len(pw) > 12:
print("This password is too long please Re-Enter another password")
continue # if you don't use continue all lines will be executed even if len > 12
else:
pwok = 1
if caps >= 1:
if lower >= 1:
if nums >= 1:
print(scr[1])
elif nums < 1:
print("your password is " + scr[1])
elif lower < 1:
print("your password strength is " + scr[1])
elif caps < 1:
print("your password strength is " + scr[1])
if caps < 1:
if lower < 1:
if nums <1:
print(scr[3])
答案 2 :(得分:0)
import re
pwok = 0
print ("You password must contain lower case and uppcase characters")
print ("It should also contain numbers as well")
while pwok != 1:
pw = raw_input("Please enter a password to test:")
caps = sum(1 for c in pw if c.isupper())
lower = sum(1 for c in pw if c.islower())
nums = len(re.search(r'\d+', pw).group())
scr = ['weak', 'medium', 'strong']
r = [caps, lower, nums]
if len(pw) < 6:
print("This password is too short please Re-Enter another password")
elif len(pw) > 12:
print("This password is too long please Re-Enter another password")
else:
pwok = 1
if caps >= 1:
if lower >= 1:
if nums >= 1:
print(scr[1])
elif nums < 1:
print("your password is " + scr[1])
elif lower < 1:
print("your password strength is " + scr[1])
elif caps < 1:
print("your password strength is " + scr[1])
if caps < 1:
if lower < 1:
if nums <1:
print(scr[3])