Why is the state not changing when something is entered incorrectly?

import random
state = 1
balance = random.randint(500,2500)
print (balance)
 
accounts= {
 "swindrunner" : "1234",
 "istormrage" : "abcd",
 "tpgallywix" : "qwerty"
}
print (state)

while state == 1:
 print (state)
 
 userpass = ["swindrunner1234", "istormrageabcd", "tpgallywixqwerty"]
 username = input("Username:")
 password = input("Password: ")
 #AccountInfo = username + password
 print(userpass)

 break

AccountInfo = username + password
state = 2

print(state)

while state == 2:
 print(state)
 print(AccountInfo)

 if AccountInfo == "swindrunner1234" or "istormrageabcd" or "tpgallywixqwerty":
  print ("ok")
  state = 3
 else:
  print ("no")
  state = 1
 break
 
print(state)

while state == 3:
 input("Display, Withdraw, Deposit, Exit")

why is the code going to state == 3 if the AccountInfo isn’t one of the 3 that its supposed to be equal to? shouldn’t the if else statement set the state == 1 if AccountInfo is incorrect?

Hello,

the issue is with the if conditional statement. It is true for any string, float or integer.

AccountInfo = 8.55  # Substitute with any "int","float" or "string" value and
                    # observe which "if" conditional statement is executed

# Original method not working - do not use - true for any value
if AccountInfo == "swindrunner1234" or "istormrageabcd" or "tpgallywixqwerty":
    print('Via original method, AccountInfo is: ', AccountInfo)


# Use this method
if AccountInfo in ("swindrunner1234", "istormrageabcd", "tpgallywixqwerty"):
    print('Via second method, AccountInfo is: ', AccountInfo)

The expression

AccountInfo == "swindrunner1234" or "istormrageabcd" or "tpgallywixqwerty"

is equivalent to

(AccountInfo == "swindrunner1234") or ("istormrageabcd") or ("tpgallywixqwerty")

and always evaluates to True.
To avoid having to repeat AccountInfo == you could use

if AccountInfo in ["swindrunner1234",  "istormrageabcd", "tpgallywixqwerty"]:
2 Likes

It’s not equivalent. It’s:

if a or b or c:

If one of them is True then it succeeds. What you want is:

if AccountInfo == "swindrunner1234" or AccountInfo == "istormrageabcd" or AccountInfo == "tpgallywixqwerty":

Or, as @bverheg put it:

if AccountInfo in ["swindrunner1234",  "istormrageabcd", "tpgallywixqwerty"]:

The first one evaluates each individually:

First: AccountInfo == "swindrunner1234"
Second: "istormrageabcd"
Third: "tpgallywixqwerty"

Since the second evaluates to True the if condition will be met.

this worked, thanks so much!