print("welcome to the password reset program")
print("please enter the new password")
input("type your new password")
input("type your new password (8-16 characters)")
password = input("type your new password (8-16 characters)")
if password == <8
print("this is too short, rethink, and re-enter your new password...")
if password == >16
print("this is too long, rethink, and re-enter your new password...")
if password == 8-16
print("this is the correct length, now re-enter to confirm")
password2 = input("re-enter your password")
if password == password2
print("your password has been changed")
if password != password2
print("this is not the same as your first password")
我收到错误“if password ==&lt; 8” 你可以帮我吗????
答案 0 :(得分:2)
您需要使用len()
来比较密码的长度。
您还需要使用<
或<=
来比较它而不是==
,并在if语句的末尾添加一个冒号。
所以你的代码看起来像是:
if len(password) <= 8:
print("this is too short, rethink, and re-enter your new password...")
if len(password) >=16:
print("this is too long, rethink, and re-enter your new password...")
你可以简化为
if len(password) <= 8 or len(password) >= 16:
print("Password must be between 8 and 16 characters, enter your new password...")
要让用户重新输入密码,您可以使用while
循环直到他们的输入有效:
while(len(password) <= 8 or len(password) >= 16):
print("Password must be between 8 and 16 characters, enter your new password...")
password=input("type your new password")
您可以再次使用它来检查用户输入相同的密码:
password2 = input("re-enter your password")
while password2!=password:
password2=input("this is not the same as your first password")
print("your password has been changed")
正如其他用户所提到的,你应该看看哈希: LxLite
答案 1 :(得分:0)
首先,没有&#39; x ==&lt; ý&#39 ;.您正在寻找&#39; x&lt; = y&#39;。
其次,正如我之前的答案中所述,你必须添加&#39;:&#39;在if之后。
你应该阅读文档。
答案 2 :(得分:-1)
print("welcome to the password reset program")
print("please enter the new password")
input("type your new password")
input("type your new password (8-16 characters)")
password = input("type your new password (8-16 characters)")
if len(password) <= 8:
print("this is too short, rethink, and re-enter your new password...")
elif len(password) >= 16:
print("this is too long, rethink, and re-enter your new password...")
else:
print("this is the correct length, now re-enter to confirm")
password2 = input("re-enter your password")
if password == password2:
print("your password has been changed")
else:
print("this is not the same as your first password")
正如其他人所建议我在python中阅读有关缩进的文档以及如何使用if / else。
答案 3 :(得分:-1)
您的代码存在一些问题,我猜测您可能来自JavaScript背景。
问题如下。
:
声明末尾的if
。== <
或== >
正确使用<=
和>=
。if
语句也有不正确的缩进。以下更新的代码。
print("welcome to the password reset program")
print("please enter the new password")
input("type your new password")
input("type your new password (8-16 characters)")
password = input("type your new password (8-16 characters)")
if len(password) <= 8:
print("this is too short, rethink, and re-enter your new password...")
if len(password) >= 16:
print("this is too long, rethink, and re-enter your new password...")
if len(password) == (8-16): # is this a range or 8 - 16?
print("this is the correct length, now re-enter to confirm")
password2 = input("re-enter your password")
if password == password2:
print("your password has been changed")
if password != password2:
print("this is not the same as your first password")
答案 4 :(得分:-1)
:
。== <
或== >
不存在,您可以使用=>
或<=
代替if password == 8-16
您可以使用运算符in
来评估变量是否在列表中,并将其作为if len(password) in range(8-16)
在你的情况下可能是
if password <= 8
print("this is too short, rethink, and re-enter your new password...")
if password >= 16
print("this is too long, rethink, and re-enter your new password...")
if len(password) in range(8, 16)
print("this is the correct length, now re-enter to confirm")
password2 = input("re-enter your password")
if password == password2
print("your password has been changed")
if password != password2
print("this is not the same as your first password")