[英]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 背景。
問題如下。
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")
:
。== <
或== >
不存在,您可以使用=>
或<=
代替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.