1

I'm wondering how I could display the same result if the user inputs two specific answers in the input command (ie 'end' and 'End').

Here's the code I have so far; everything is working except the "end" part:

def password(): #defines password
print("Please input your password.")
pass2 = input("Password: ")
if pass2 == ("abcd123"):
    print("Thank you for using Efficient Systems, Co.")
    end = input("Type end to exit: ")
    while True:
        if end == ("end") and ("End"):
            break
        else:
            print("Invalid command.")
            end = input("Type end to exit: ")
else:
    print("Invalid command.")
    time.sleep(1)
    pass2 = input("Password: ")

I think it's worth noting that if I do type in 'end', it goes back to pass2.

lvc
  • 34,233
  • 10
  • 73
  • 98
Wai-man
  • 21
  • 2

2 Answers2

4

The condition is wrong -

if end == ("end") and ("End"):

This would only evaluate to true (when end is 'end' ), because this would get translated as -

if ((end == "end") and "End")

And any non-empty string in python is True-like (has true value in boolean context.)

To check for all cases, best thing to do would be to check your variable's .lower() with 'end' , example -

if end is not None and end.lower() == "end":

And since you are taking input using input() function, you actually do not need the is not None part, so you can do -

if end.lower() == "end":
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

You need to use or:

if end == "end" or end == "End":
    ....

if you want to check the string case-insensitive, use str.casefold:

if end.casefold() == "end":
    ...
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • I attempted if end.casefold == "end": --- but it comes back saying the following error: AttributeError: 'str' object has no attribute 'casefold'. What could I do? – Wai-man Jul 30 '15 at 01:05
  • Actually, I found the problem; I'm using Python 3.0. I'll try this with the newest Python version. – Wai-man Jul 30 '15 at 01:07
  • @Wai-man, You can use `end.lower() == "end"' or `end.upper() == "END"` – falsetru Jul 30 '15 at 01:11