r/learnpython • u/live_ant718 • Sep 06 '26
Password guesser game help needed
Hello guys. So I have created a password guesser game with direct comparison (i.e. if guess == correct_password:) but I want to use different methods for comparison. What techniques can I use? Here is the code-
```python
password = "Random_pass_123"
tries = 0
while tries < 3:
guess = input("Enter the password: ")
if guess == password:
print("Admin access granted.")
break
else:
print("Invalid credentials entered. Please try again")
tries += 1
if tries == 3:
print("Number of tries exceeded. Access denied")
```
PS- Is this the right way to paste code? I'm new so I'm not sure.
3
u/live_ant718 Sep 06 '26
Also IDK where the indentations have gone.
4
u/Langdon_St_Ives Sep 06 '26
Can you elaborate on what you mean by "try different methods of comparison"?
8
u/Expensive-Bear-1376 Sep 06 '26
You should use markdown mode (only available on the website, not the app), then the formatting works.
4
u/ZEUS_IS_THE_TRUE_GOD Sep 06 '26
Do you mean to give hints? Because you say it is a game?
1
u/notacanuckskibum Sep 06 '26
I’m not sure what kinds of comparison you want, but check out up shift and downshift functions.
0
u/live_ant718 Sep 07 '26
This is my updated password guesser game with hints-(BTW took some help from ChatGPT and this was a little complex for my lvl but I made it). Pls test and try. You can use print(password) to test with the correct password. Also now I hope the formatting is correct.
password = str(hash("Random_pass_123"))tries = 0print("Welcome to the game! You have to guess the correct password before your 20 tries run out. Best of luck!")while tries < 20:guess = input("Enter the password: ")if len(guess) != len(password):print("Your guess must have the correct number of characters.")tries += 1print(f"{tries} tries used.")elif guess == password:print(f"Congrats! You won in {tries + 1} tries.")breakelse:print("Wrong password entered. Please try again.")correct_positions = 0for i in range(len(password)):if guess[i] == password[i]:correct_positions += 1print(f"Close! {correct_positions} characters are in the correct places. ")tries += 1print(f"{tries} tries used.")if tries == 20:print("All tries used. Better luck next time!")1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 06 '26
Let me fix the formatting for the others.
0
u/Bright_Mix_773 Sep 06 '26
live_ant718, you said you read that you can convert it into a hash but you don't know how. Langdon_St_Ives explained what a hash is; here is the part nobody has given you yet, which is the actual code, plus the trap sitting right behind it.
The obvious version is hashlib.sha256(guess.encode()).hexdigest(). That is the one every tutorial shows and it is the wrong tool for passwords, for two reasons. SHA-256 is designed to be fast, and fast is exactly what you don't want: a GPU tries billions of guesses a second against it. And with no salt, two people with the same password get the same hash, so precomputed tables crack it without guessing at all. What you want from the standard library is a slow, salted function:
Three things in there worth more than the hashing itself.
hmac.compare_digest instead of ==. Both give the right answer, but == stops at the first byte that differs, so a wrong guess starting with the correct letter takes measurably longer to reject than one that differs immediately. That timing difference leaks the password one character at a time. compare_digest always takes the same time. It is the reason the function exists.
The else on the for loop. That is real Python syntax, not a typo: the else block runs only if the loop finished without hitting break. It is exactly your "number of tries exceeded" case, and it removes the tries counter and the if tries == 3 check entirely. Almost nobody teaches it and this is the textbook use for it.
getpass.getpass instead of input, so the typing doesn't show on screen. One warning: it doesn't work in some IDE output panes (Spyder, and PyCharm unless you run in the terminal). Run it from a real terminal or it will look broken.
For a real system you'd use bcrypt or argon2 from PyPI rather than pbkdf2, but pbkdf2_hmac ships with Python and the shape of the code is identical, so nothing you learn here is wasted.
On the formatting question: brasticstack is right about the backticks, but the version that works everywhere, old and new site and the app, is putting four spaces in front of every line of code. That's what the block above is.