Python 在while循环中定义变量
我正在学习一门python课程,我一直在努力在代码中正确地使用while循环 我的代码应该检查一个密码,如果它的最小长度为6,最大长度为14,它还将检查密码是否只有数字或字母。如果它有两种密码的组合,它应该打印“强密码”,如果它只有数字或字母,它将打印“弱密码”Python 在while循环中定义变量,python,Python,我正在学习一门python课程,我一直在努力在代码中正确地使用while循环 我的代码应该检查一个密码,如果它的最小长度为6,最大长度为14,它还将检查密码是否只有数字或字母。如果它有两种密码的组合,它应该打印“强密码”,如果它只有数字或字母,它将打印“弱密码” MIN\u密码长度=6 最大密码长度=14 当password_length>=MIN_password_length或password_length时,您的想法或多或少是正确的。只是需要在循环外部为password\u length分
MIN\u密码长度=6
最大密码长度=14
当password_length>=MIN_password_length或password_length时,您的想法或多或少是正确的。只是需要在循环外部为password\u length
分配一个值
想想看:当你的代码运行时,解释器点击while
循环并尝试进行涉及密码长度的比较。但是,此时还不存在密码\u length
,因为它第一次获得值时就在循环中。因此,在进入循环之前,应该将其初始化为一个合理的值,例如0
补充两点:
您正在计算上一个密码的密码长度,因此,如果输入的密码太短/太长,然后输入一个可接受的密码,则打印的长度将是不可接受的长度
一般来说,更喜欢f-strings或str.format
调用字符串连接,因此,对于print
,这可能更好:
在while循环之前,使用类似“password_length=MAX_password_length”的内容初始化,否则while循环无法启动。while循环中的第一行是“password_length=len(password)”,它将正确设置password_length的值,但是while循环需要从某个地方开始,以便您可以到达该点。while循环中的条件是错误的。你可以尝试:
MIN\u密码长度=6
最大密码长度=14
密码=输入(“输入密码:”)
密码长度=len(密码)
密码长度<最小密码长度>最大密码长度:
打印(“密码长度”、“密码长度”、“不正确”。必须介于”、“最小密码长度”和“最大密码长度”之间)
密码=输入(“再次输入密码:”)
密码长度=len(密码)
如果password.isalpha()或password.isnumeric():
打印(“您的密码很弱!”)
其他:
打印(“您的密码很强!”)
打印(“密码中使用的字符数:”,密码长度,”。预期的最小长度为:“,最小密码长度,”。最大长度为:”,最大密码长度)
在定义变量之前,您正在使用密码
和密码长度
变量
您还可以使用函数并将其重写为更结构化的:
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
def checkPass():
password = input("Enter your password: ")
password_length = len(password)
if password.isalpha():
print("Your password is weak!")
elif password.isnumeric():
print("Your password is weak!")
else:
print("Your password is strong!")
return password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH
while checkPass():
continue
print("Number of characters used in password: ", password_length,"the min length expected is: ",MIN_PASSWORD_LENGTH, "the max length is: ", MAX_PASSWORD_LENGTH)
MIN\u密码长度=6
最大密码长度=14
def checkPass():
密码=输入(“输入密码:”)
密码长度=len(密码)
如果密码为.isalpha():
打印(“您的密码很弱!”)
elif password.isnumeric():
打印(“您的密码很弱!”)
其他:
打印(“您的密码很强!”)
return password\u length>=MIN\u password\u length或password\u length问题在于,在代码知道密码是什么之前,您正在调用密码
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
# password must be defined before you use it.
password = input("Enter your password: ")
while password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH:
password_length = len(password)
if password.isalpha():
print("Your password is weak!")
elif password.isnumeric():
print("Your password is weak!")
else:
print("Your password is strong!")
print("Number of characters used in password: ", password_length,"the min length expected is: ",MIN_PASSWORD_LENGTH,
"the max length is: ", MAX_PASSWORD_LENGTH)
MIN\u密码长度=6
最大密码长度=14
#密码必须在使用前定义。
密码=输入(“输入密码:”)
当password_length>=MIN_password_length或password_length时,这是一个基于我理解的版本
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
password = ""
atLeast1Apha = False
atLeast1Numeric = False
while True:
password = input("Enter your password: ")
if len(password) > MAX_PASSWORD_LENGTH or len(password) < MIN_PASSWORD_LENGTH:
print("Password could be between %s and %s long" %(MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH))
continue
for char in password:
if char.isalpha():
atLeast1Apha = True
if char.isdigit():
atLeast1Numeric = True
break
if atLeast1Apha and atLeast1Numeric:
print("Your password is strong!")
else:
print("Your password is weak!")
MIN\u密码长度=6
最大密码长度=14
password=“”
至少1APHA=错误
至少1numeric=False
尽管如此:
密码=输入(“输入密码:”)
如果len(密码)>最大密码长度或len(密码)<最小密码长度:
打印(“密码长度可能介于%s和%s之间”%(最小密码长度、最大密码长度))
持续
对于密码中的字符:
如果char.isalpha():
至少1apa=真
如果char.isdigit():
至少1numeric=True
打破
如果至少1个PHA和至少1个数字:
打印(“您的密码很强!”)
其他:
打印(“您的密码很弱!”)
代码块可以使用缩进。(*提示:在定义密码之前,您正在使用密码长度。)还建议您阅读。
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
# password must be defined before you use it.
password = input("Enter your password: ")
while password_length >= MIN_PASSWORD_LENGTH or password_length <= MAX_PASSWORD_LENGTH:
password_length = len(password)
if password.isalpha():
print("Your password is weak!")
elif password.isnumeric():
print("Your password is weak!")
else:
print("Your password is strong!")
print("Number of characters used in password: ", password_length,"the min length expected is: ",MIN_PASSWORD_LENGTH,
"the max length is: ", MAX_PASSWORD_LENGTH)
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 14
password = ""
atLeast1Apha = False
atLeast1Numeric = False
while True:
password = input("Enter your password: ")
if len(password) > MAX_PASSWORD_LENGTH or len(password) < MIN_PASSWORD_LENGTH:
print("Password could be between %s and %s long" %(MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH))
continue
for char in password:
if char.isalpha():
atLeast1Apha = True
if char.isdigit():
atLeast1Numeric = True
break
if atLeast1Apha and atLeast1Numeric:
print("Your password is strong!")
else:
print("Your password is weak!")