-1

I am very new to Python. I was creating a program to begin work on something big that I believe would be revolutionary. When I saved my code and tested it, however, it remained in the infinite loop I had created even after I typed something that did not match the conditions! Could I please get some help?

Code:

*...snip...* print("You may now choose a password. Your password must: \n"
      + "-be at least 8 characters \n"
      + "-NOT be 'password' \n"
      + "-NOT be an ordered list of numbers \n")
print("What is your password?")
password = input()
if password == "password" or "Password" or "PASSWORD" or "12345678":
    while password == "password" or "Password" or "PASSWORD" or "12345678":
        print("That password is too weak. Choose another password.")
        password = input()
else: *...snip...*
Tanner S.
  • 3
  • 3
  • You need to do it like `if password == "password" or password == "Password" or password == "PASSWORD" or password == "12345678":` or more succinctly `if password.lower() in ("password", "12345678"):`. As it is all the later conditions always evaluate to True and so your loop keeps on going. This is a common beginner mistake. – Paul Rooney Jun 16 '21 at 23:30
  • Hi there, the problem is that when you are checking if `password == "password"` it is ok but than you must rewrite `password ==`, right now it just tries to evaluate the phrase `or "Password"` which is not `True` so it keeps looping. In order to create a better looking code you might use: `password in ["password", "Password", "PASSWORD", "12345678"]` – Yonlif Jun 16 '21 at 23:31

1 Answers1

1

You have to write

if password == "password" or password == "Password" or password == "PASSWORD" or password == "12345678":

Your if evaluates your statement like this : if password is equal to the string "password" or if the string "Password" is different from null and so on.

  • 3
    Simpler to test `if password in ('password', 'Password', 'PASSWORD', '12345678'):`; doesn't involve repeating `or password ==` over and over (faster too, since the values to test are all literals; CPython at least will build and cache the `tuple`, so the work involved is just loading the constant `tuple` and asking it to do a containment test, which will short-circuit internally just like the chain of `or`s will). – ShadowRanger Jun 16 '21 at 23:36
  • that is correct, thanks for observation – Cezar Todirisca Jun 16 '21 at 23:37
  • 1
    I would do if password.lower() == "password" or password == "12345678". That way it can't be the word password no matter what letters are capitalized. – Peter Jones Jun 16 '21 at 23:44
  • 1
    @PeterJones: Wait, you're telling me `"pASsWorD"` is an insecure password?!? *hurriedly changes all my passwords* :-) I'd still use `in` there, just to avoid typing `password` so much, e.g. `if password.lower() in ("password", "12345678"):`, but yes, I agree that logically any capitalization mix on "password" should be rejected as insecure. – ShadowRanger Jun 18 '21 at 01:31
  • @ShadowRanger That's a great point. In would be better, I like the tuple Idea more the more I see it. – Peter Jones Jun 18 '21 at 07:06