So the exercise I'm doing goes:
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”.
My attempt looks like this:
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.")
That didn't give me the result I was hoping for though. This should be really simple but as you can tell I'm a beginner:) Thank you in Advance!
Marcel
That passw.lower
is a method, the method itself, you may call it to have the password in lowercase. Also remove passw != passw2
in second ìf
that's mandatory True
if passw == passw2:
print("Thank you.")
elif passw.lower() == passw2.lower():
print("They must be in the same case.")
else:
print("Incorrect.")
More
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'>
The problem is that your elif
condition always evaluates to True
:
elif passw != passw2 and passw.lower == passw2.lower:
str.lower
is a function, and comparing a function with the same function logically ends up being True
. You have to call the functions instead and compare their results. Additionally, you are doing comparing passw
and passw
twice: once you check if they are the same in the if
condition, and once you check if they are not the same in the elif
condition. This is useless, because the elif
condition will only ever be executed when the if
condition was False
. Following the working code:
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.")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.