-
Notifications
You must be signed in to change notification settings - Fork 0
/
text based hangman game.py
65 lines (53 loc) · 1.84 KB
/
text based hangman game.py
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
import random
# List of secret words
word_bank = ["python", "java", "javascript", "ruby", "php", "csharp", "swift", "html", "css", "sql"]
# Select a random word from the word bank
secret_word = random.choice(word_bank)
# Initialize variables
guesses = ''
turns = 10
letters_guessed = []
# Display welcome message and rules
print("Welcome to Hangman!")
print("You have 10 turns to guess the secret word.")
print("If you guess a letter that is not in the secret word, you will lose a turn.")
print("If you guess the same letter twice, it will not count against you.")
print("Good luck!\n")
# Loop until the player wins or runs out of turns
while turns > 0:
# Display the current progress
progress = ''
for letter in secret_word:
if letter in guesses:
progress += letter
else:
progress += '_'
print(progress)
# Get the player's guess
guess = input("Guess a letter: ").lower()
# Check if the guess is valid
if not guess.isalpha() or len(guess) != 1:
print("Invalid guess. Please enter a single letter.")
continue
# Check if the letter has already been guessed
if guess in letters_guessed:
print("You already guessed that letter. Please try again.")
continue
else:
letters_guessed.append(guess)
# Check if the guess is correct
if guess in secret_word:
print("Correct!")
guesses += guess
else:
print("Incorrect!")
turns -= 1
# Check if the player has won
if '_' not in progress:
print("Congratulations, you won!")
break
# Display the remaining turns
print("You have", turns, "turns left.")
# Display the secret word if the player has lost
if turns == 0:
print("Sorry, you lost. The secret word was", secret_word)