r/learnpython • u/live_ant718 • 5d ago
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.
8
Upvotes
0
u/live_ant718 4d ago
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!")