username = input("Welcome, please enter your username.")
password = input("Please enter your password.")
if username != "tony" and password != "password123":
print("Access Denied.")
else:
print("Welcome to the jungle")
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Change the test to:
if username != "tony" or password != "password123":
or turn the whole thing around:
if username == "tony" and password == "password123":
print("Welcome to the jungle")
else:
print("Access Denied.")
That (IMO) makes it more clear both the username and the password need to be correct.
Method 2
Will try to make you understand by AND gate.
| A | B | C | D | E |
|---|---|---|---|---|
| username | password | username!=Tony | password!=password123 | Colmn_C AND Colmn_D(Access denied) |
| Tony | password123 | FALSE | FALSE | FALSE |
| Hulk | Password123 | TRUE | FALSE | FALSE |
| Tony | xyz | FALSE | TRUE | FALSE |
| Hulk | xyz | TRUE | TRUE | TRUE |
So you can see ,why Access is denied only when both are incorrect
Method 3
Good news, your code is written correctly!.
Bad news, your code is not doing what you want.
Try to read your conditional out load.
Your computer: ‘if the user name is not “tony” and the password is not “password123,” I should deny access.
Everything else I should accept.’
That means, when only one of them is wrong, it is ok!
Method 4
just change the statements or change the condition AND with OR
solution1
username = input("Welcome, please enter your username.")
password = input("Please enter your password.")
if username != "tony" or password != "password123":
print("Access Denied.")
else:
print("Welcome to the jungle")
solution2
username = input("Welcome, please enter your username.")
password = input("Please enter your password.")
if username != "tony" and password != "password123":
print("Welcome to the jungle")
else:
print("Access Denied.")
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0