我如何使这项工作?我不断收到语法错误

时间:2017-01-06 17:13:37

标签: python

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” 你可以帮我吗????

5 个答案:

答案 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背景。

问题如下。

  1. 您遗漏了:声明末尾的if
  2. 您无法在Python中使用== <== >正确使用<=>=
  3. 您的if语句也有不正确的缩进。
  4. 以下更新的代码。

    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")
    

    不要忘记哈希密码。https://docs.python.org/3/library/hashlib.html

答案 4 :(得分:-1)

  1. 您需要在每个if,for或while语句后添加:
  2. 运营商== <== >不存在,您可以使用=><=代替
  3. 在此行中:if password == 8-16您可以使用运算符in来评估变量是否在列表中,并将其作为if len(password) in range(8-16)
  4. 进行搜索

    在你的情况下可能是

    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")