-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman
More file actions
61 lines (50 loc) · 1.61 KB
/
hangman
File metadata and controls
61 lines (50 loc) · 1.61 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
import random
import os
import platform
def clear_console():
os.system("cls")
def hangman():
clear_console()
wordlist = ["Hallo", "glück", "stark"]
word = random.choice(wordlist).lower()
guessed_word = ["_"] * len(word)
guessed_letters = []
attempts = 6
print("\nWelcome to Hangman!")
print("\n[if you want to quit press q]")
while attempts > 0:
print("\n" + " ".join(guessed_word))
print(f"Remaining attempts: {attempts}")
guess = input("Guess a letter: ").lower()
if guess == "q":
clear_console()
quit()
if len(guess) != 1 or not guess.isalpha():
print("Please enter a valid single letter.")
continue
if guess in guessed_letters:
print(f"You have already guessed '{guess}'.")
continue
guessed_letters.append(guess)
if guess in word:
print(f"Good job! '{guess}' is in the word.")
for i in range(len(word)):
if word[i] == guess:
guessed_word[i] = guess
else:
print(f"Sorry! '{guess}' is not in the word.")
attempts -= 1
if "_" not in guessed_word:
print("\n" + " ".join(guessed_word))
print("Congratulations, you guessed the word!")
break
else:
print(f"Game over!!! The word was '{word}'.")
while True:
question = input("Do you want to play? [Y][N] ").lower()
if question == "y":
hangman()
else:
print("Bye!")
clear_console()
break