2017-01-06 60 views
-3
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」 你能幫我嗎?我如何使這項工作?我不斷收到語法錯誤

+1

檢查python文檔。在if之後你必須使用':'。像這樣''if some_value:' – metmirr

+0

你在'if password == <8'行有'意想不到的縮進',並且你需要在行尾加上':' – Jaco

+0

你應該考慮看看一些Python教程。 –

回答

-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。

+0

我不介意downvotes,但評論解釋將不勝感激! – bouletta

+2

'password> = 16:'是錯誤的。另外,沒有解釋的答案不是很有用 – vaultah

+0

謝謝,我忘了第二次改變它。我同意這個解釋,但大多數已經在評論中說過了。 – bouletta

0

首先,有沒有這樣的事情爲x == < Y'。您正在尋找'x < = y'。

其次,在我之前的回答說,你必須添加「:」如果在後。

您應該閱讀文檔。

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

您可以再次使用它來檢查用戶e nters相同的密碼:

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

+0

您錯過了第3行上的「:」 – bouletta

+0

謝謝,現在修復 – Trelzevir

+1

這也值得鏈接或通知OP有關哈希。如果他們要玩密碼,他們應該知道哈希。 –

-1

你的代碼有一些錯誤,我猜你可能來自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

+0

我不確定爲什麼我在技術上正確並且回答用戶問題時得到了我的答案的投票。 –

+0

'(8-16)'不是範圍 – vaultah

+0

我沒有說這是。我做了一個內聯評論,詢問這是OP在尋找什麼。 –

-1
  1. 您需要在每個if,for或while語句後添加:
  2. 運營商== <== >犯規存在,你可以使用=><=代替
  3. 在這一行:if password == 8-16可以使用運營商in,以評估該變量是在列表中,souch爲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") 
+1

縮進是錯誤的,您沒有在任何地方添加':',並且比較'password'和整數是沒有意義的。 – vaultah

相關問題