2

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

2
  • One minor change, if passw == passw2 fails in first if case, do we still need to check passw != passw2 in other elif case. Commented May 8, 2021 at 10:07
  • @ChiragPatel we dont need to check that again … Commented May 8, 2021 at 10:36

2 Answers 2

3

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'>
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.