python - 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)
解决方案
您正在比较常量变量,您应该使用:
if password_length >= MIN_PASSWORD_LENGTH:
...
...
推荐阅读
- perl - How to automatically install all dependendicies in cpan Bio
- c++ - 返回时不允许隐式转换
- rstudio - java.lang.OutOfMemoryError: GC overhead limit exceeded using R
- php - 无法在 json 响应中输出会话变量
- javascript - 从派生自 SQL 查询的 URL 打开页面
- python - 如何使用python和opencv在图像上绘制两个轮廓,它们之间的轮廓距离为100像素
- python - 如果我的文本文件在 Python 中有整数和字符串,我该如何排序
- android - RecyleView 显示所有重复项
- javascript - ES6 导入场景
- html - Vue Native Router 出现未使用错误