簡體   English   中英

我如何使這項工作? 我不斷收到語法錯誤

[英]How do i make this work? i keep getting Syntax errors

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

我收到“如果密碼== <8”的錯誤,你能幫我嗎????

您需要使用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")

正如其他用戶所提到的,您應該查看散列: https : //docs.python.org/3/library/hashlib.html

首先,沒有“x== < y”這樣的東西。 您正在尋找“x <= y”。

其次,如我之前的答案所述,您必須在 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...")
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 的文檔。

您的代碼有一些問題,我猜您可能有 JavaScript 背景。

問題如下。

  1. 您缺少if語句末尾的:
  2. 你不能在 Python 中使用== <== > ,正確使用的是<=>=
  3. 您的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")

不要忘記散列密碼。 https://docs.python.org/3/library/hashlib.html

  1. 您需要在每個 if、for 或 while 語句之后添加:
  2. 運算符== <== >不存在,您可以使用=><=代替
  3. 在這一行中: 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM