-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.py
More file actions
47 lines (35 loc) · 1.23 KB
/
Copy pathHangman.py
File metadata and controls
47 lines (35 loc) · 1.23 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
from words import words
import string
import random
def get_valid(words):
word = random.choice(words)
while "-" in word or " " in word:
word = random.choice(words)
return word.upper()
def hangman():
word = get_valid(words)
word_letters = set(word)
alphabet = set(string.ascii_uppercase)
used_letters = set()
lives = 6
while len(word_letters) > 0 and lives > 0:
print(f"\ncurrent lives: {lives} You have used these letters: ", " ".join(used_letters))
word_list = [letter if letter in used_letters else "-" for letter in word]
print("Current word: ", " ".join(word_list))
user_letter = input("Enter letter: ").upper()
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
else:
lives -= 1
print("Letter is not in the word")
elif user_letter in used_letters:
print("you already used it")
else:
print("Invalid character")
if lives == 0:
print("You lose")
else:
print("You won")
hangman()