-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
60 lines (44 loc) · 1.56 KB
/
game.py
File metadata and controls
60 lines (44 loc) · 1.56 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
import random
game_title = "Word Raider"
word_bank = []
with open("words.txt") as word_file:
for w in word_file:
word_bank.append(w.rstrip().lower())
word_to_guess = random.choice(word_bank)
max_turns = 5
turns_taken = 0
misplaced_letters = []
incorrect_letters = []
print(f"Welcome to {game_title}!")
print(f"You have {max_turns} attempts to guess the word.")
while turns_taken < max_turns:
guess = input("Guess a word: ").lower()
if len(guess) != len(word_to_guess) or not guess.isalpha():
print("Please enter 5-letter word.")
else:
index = 0
for c in guess:
if c == word_to_guess[index]:
print(c, end=" ")
if c in misplaced_letters:
misplaced_letters.remove(c)
elif c in word_to_guess:
if c not in misplaced_letters:
misplaced_letters.append(c)
print("_", end=" ")
else:
if c not in incorrect_letters:
incorrect_letters.append(c)
print("_", end=" ")
index += 1
print("\n")
print("Misplaced letters: ", misplaced_letters)
print("Incorrect letters: ", incorrect_letters)
turns_taken += 1
if guess == word_to_guess:
print("Congratulations, you win!")
break
if turns_taken == max_turns:
print("Sorry, you lost. The word was", word_to_guess)
break
if (turns_taken == max_turns): print(f"Sorry, you've used all your attempts. The word was '{word_to_guess}'.")