-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_guess.py
More file actions
49 lines (42 loc) · 1.96 KB
/
password_guess.py
File metadata and controls
49 lines (42 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# This is a simple password guessing game implemented in Python. The game allows the user to choose a difficulty level and provides hints based on the chosen level. The user can keep guessing until they find the correct password, and the game will provide feedback on each guess.
import random
easy_words = ["apple", "house", "cat", "dog", "book"]
medium_words = ["python", "guitar", "flower", "window", "bottle"]
hard_words = ["xylophone", "quizzical", "mnemonic", "pneumonia", "juxtapose"]
print("Welcome to the Password Guessing Game!")
print("Try to guess the password. You can choose a difficulty level to get a hint. Types of hints:")
print("1- Easy: Get a hint about the length of the password.")
print("2- Medium: Get a hint about the first letter of the password.")
print("3- Hard: Get a hint about the last letter of the password.")
difficulty = input("Choose a difficulty level (1, 2, 3): ")
if difficulty == "1":
password = random.choice(easy_words)
print(f"Hint: The password has {len(password)} letters.")
elif difficulty == "2":
password = random.choice(medium_words)
print(f"Hint: The password starts with '{password[0]}'.")
elif difficulty == "3":
password = random.choice(hard_words)
print(f"Hint: The password ends with '{password[-1]}'.")
else:
print("Invalid difficulty level. Defaulting to easy.")
password = random.choice(easy_words)
print(f"Hint: The password has {len(password)} letters.")
attempts = 0
print("Start guessing the password!")
while True:
guess = input("Enter your guess: ")
attempts += 1
if guess == password:
print(f"Congratulations! You've guessed the password '{password}' in {attempts} attempts!")
break
else:
print("Wrong guess. Try again!")
hint = ""
for i in range(len(password)):
if i < len(guess) and guess[i] == password[i]:
hint += guess[i]
else:
hint += "_"
print(f"Hint: {hint}")
print("Game over. Thanks for playing!")