【发布时间】:2019-03-17 01:10:55
【问题描述】:
我刚开始使用 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 语句中使用的是
MIN_PASSWORD_LENGTH而不是password_length,因此它将始终测试6 >= 6。你的意思是if password_length >= MIN_PASSWORD_LENGTH
标签: python if-statement