Skip to content

Commit 01b7e57

Browse files
committed
Added a new game - Guess the word
1 parent 98e7011 commit 01b7e57

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import random
2+
3+
# List of words for the game
4+
word_list = ["python", "java", "javascript", "ruby", "php", "html", "css", "csharp", "angular", "golang", "c", "dotnet", "perl", "rust", "scala", "dart", "fortran", "cobol", "haskell"]
5+
6+
# Function to choose a random word from the list
7+
def choose_random_word(word_list):
8+
return random.choice(word_list)
9+
10+
# Function to play the word guessing game
11+
def word_guessing_game():
12+
word_to_guess = choose_random_word(word_list)
13+
guessed_letters = []
14+
attempts = 6
15+
16+
print("Welcome to the Word Guessing Game!")
17+
print("You have 6 attempts to guess the word.")
18+
print("_ " * len(word_to_guess))
19+
20+
while attempts > 0:
21+
guess = input("Guess a letter: ").lower()
22+
23+
if len(guess) != 1 or not guess.isalpha():
24+
print("Please enter a single letter.")
25+
continue
26+
27+
if guess in guessed_letters:
28+
print("You've already guessed that letter.")
29+
continue
30+
31+
guessed_letters.append(guess)
32+
33+
if guess in word_to_guess:
34+
print("Correct guess!")
35+
remaining_letters = [letter if letter in guessed_letters else '_' for letter in word_to_guess]
36+
print(" ".join(remaining_letters))
37+
if "_" not in remaining_letters:
38+
print("Congratulations! You've guessed the word:", word_to_guess)
39+
break
40+
else:
41+
attempts -= 1
42+
if attempts < 3:
43+
print(f"It is a name of a programming language")
44+
print(f"Wrong guess. You have {attempts} attempts remaining.")
45+
46+
if attempts == 0:
47+
print("You've run out of attempts. The word was:", word_to_guess)
48+
49+
# Start the game
50+
word_guessing_game()

0 commit comments

Comments
 (0)