![](/img/trans.png)
[英]Python compare two different CSV Files which values are not in the same rows
[英]how to compare if two values are the same but different cases in python
所以我正在做的練習是:
086 要求用戶輸入新密碼。 請他們再次輸入。 如果兩個密碼匹配,顯示“謝謝”。 如果字母正確但大小寫錯誤,則顯示消息“它們必須大小寫相同”,否則顯示消息“不正確”。
我的嘗試如下所示:
passw = input("Enter password: ")
passw2 = input("Enter password again: ")
if passw == passw2:
print("Thank you.")
elif passw != passw2 and passw.lower == passw2.lower:
print("They must be in the same case.")
else:
print("Incorrect.")
但這並沒有給我我希望的結果。 這應該很簡單,但你可以告訴我我是一個初學者:) 提前謝謝你!
馬塞爾
passw.lower
是一個方法,方法本身,你可以調用它來獲得小寫的密碼。 還要刪除passw != passw2
在第二個ìf
如果這是強制性的True
if passw == passw2:
print("Thank you.")
elif passw.lower() == passw2.lower():
print("They must be in the same case.")
else:
print("Incorrect.")
更多的
passw = "Abc"
print(passw.lower(), "/", type(passw.lower()))
# abc / <class 'str'>
print(passw.lower, "/", type(passw.lower))
# <built-in method lower of str object at 0x00000202611D4730> / <class 'builtin_function_or_method'>
問題是您的elif
條件始終評估為True
:
elif passw != passw2 and passw.lower == passw2.lower:
str.lower
是 function,將 function 與相同的 function 進行比較邏輯上最終為True
。 您必須改為調用函數並比較它們的結果。 此外,您正在比較passw
和passw
兩次:一次在if
條件下檢查它們是否相同,一次在elif
條件下檢查它們是否不同。 這是沒有用的,因為只有當if
條件為False
時才會執行elif
條件。 遵循工作代碼:
passw = input("Enter password: ")
passw2 = input("Enter password again: ")
if passw == passw2:
print("Thank you.")
elif passw.lower() == passw2.lower():
print("They must be in the same case.")
else:
print("Incorrect.")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.