-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword.py
More file actions
105 lines (81 loc) · 2.84 KB
/
Copy pathword.py
File metadata and controls
105 lines (81 loc) · 2.84 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import random
import time
def get_word(difficulty):
if difficulty == "easy":
words = ["apple", "baker", "candy", "drama", "eagle"]
elif difficulty == "medium":
words = ["juice", "image", "lemon", "melon", "music"]
else:
words = ["python", "shell", "heart", "swift", "unity"]
return random.choice(words)
def check_guess(guess, word):
correct_positions = []
correct_letters = []
for i in range(len(guess)):
if guess[i] == word[i]:
correct_positions.append(i)
elif guess[i] in word:
correct_letters.append(guess[i])
return correct_positions, correct_letters
def play_game():
difficulty = input("Choose a difficulty level: easy, medium, or hard? ")
word = get_word(difficulty)
guesses = 0
start_time = time.time()
while guesses < 6:
guess = input("Enter your guess: ")
guess = guess.lower()
if len(guess) != 5:
print("Please enter a 5-letter word.")
continue
correct_positions, correct_letters = check_guess(guess, word)
if len(correct_positions) == 5:
end_time = time.time()
total_time = round(end_time - start_time, 2)
print("Congratulations! You guessed the word in {} seconds!".format(total_time))
return total_time
print("Your guess:")
for i in range(len(guess)):
if i in correct_positions:
print("{} (correct position)".format(guess[i]))
elif guess[i] in correct_letters:
print("{} (correct letter)".format(guess[i]))
else:
print("{}".format(guess[i]))
guesses += 1
if guesses == 6:
print("Sorry, you didn't guess the word in time. The word was {}".format(word))
return None
def add_score(name, score):
with open("leaderboard.txt", "a") as f:
f.write("{} - {} seconds\n".format(name, score))
def show_leaderboard():
with open("leaderboard.txt", "r") as f:
leaderboard = f.readlines()
if len(leaderboard) == 0:
print("The leaderboard is empty.")
else:
print("Leaderboard:")
for line in leaderboard:
print(line.strip())
def main():
while True:
print("\nWelcome to the Word Guessing Game!\n")
print("1. Play game")
print("2. Show leaderboard")
print("3. Quit\n")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter your name: ")
score = play_game()
if score is not None:
add_score(name, score)
elif choice == "2":
show_leaderboard()
elif choice == "3":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()