python - python中如何比较两个值是否相同但情况不同

标签 python

所以我正在做的练习是:

086 Ask the user to enter a new password. Ask them to enter it again. If the two passwords match, display “Thank you”. If the letters are correct but in the wrong case, display th emessage “They must be in the same case”,otherwise display the message “Incorrect”.

我的尝试如下:

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一个方法,方法本身,你可以调用它来获得小写的密码。还要删除第二个 ìf 中的 passw != passw2 这是强制性的 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'>

关于python - python中如何比较两个值是否相同但情况不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67446309/

相关文章:

python - 无法运行简单的程序 - Python - Kivy - Ubuntu

python - 两个numpy ndarrays的字典序比较

python - Linux下如何用Python创建master进程?

python - 如何在列表理解中使用多个条件子句?

python - 如何将 startTLS 与 ldaptor 一起使用?

python - 没有实例的类 - 更多 pythonic 方式?

Python argparse 无法正确处理 Windows 中的路径

python - Django:以 10 为基数的 int() 的文字无效: 'csrfmiddlewaretoken'

python - 一种使用 Pandas 根据相应行将决策写入列的快速方法?

python - 使用缩小的公平窗口迭代数组