首页 > 解决方案 > If else 语句打印“你是对的”,即使不满足最小长度

问题描述

我刚开始使用 python,但在 python 中使用 if else 语句时遇到了一些问题。我正在尝试创建一个检查密码长度的程序。如果它匹配最小长度它应该打印一条消息说你是正确的,但是当它不匹配时它应该打印你不正确。当我运行我的代码时,即使不满足最小长度,它也只会打印出“你是正确的”。我不确定我是否正确使用了 if else 语句,是否需要使用 2 个变量才能使其工作?

MIN_PASSWORD_LENGTH = 6 #The length of the password is determined by the variable MIN_PASSWORD_LENGTH.
                    #We have assigned the variable with the value of 6.

password = input('Enter your password: ')
password_length = len(password)

if MIN_PASSWORD_LENGTH >= 6:
    print("Your password is correct!")
else:
    print("Your not correct!")

print('Number of characters used in password: ', password_length,'and the min length expected is: ',MIN_PASSWORD_LENGTH)

标签: pythonif-statement

解决方案


您正在比较常量变量,您应该使用:

if password_length >= MIN_PASSWORD_LENGTH:
    ...
    ...

推荐阅读