-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
59 lines (51 loc) · 2.07 KB
/
Copy pathhangman.py
File metadata and controls
59 lines (51 loc) · 2.07 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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 18 15:59:49 2023
@author: 01001X744
"""
import random
from words import words
import string
def get_valid_word(words):
word = random.choice(words) # randomly select a word from the list
while '-' in word or ' ' in word:
word = random.choice(words)
return word
#print(word)
#print(words)
def hangman():
word = get_valid_word(words)
word_letters = set(word) # letters in the word
alphabet = set(string.ascii_lowercase)
used_letters = set() # what the user has guessed
lives = int(len(word)) - 1
#get user input
while len(word_letters) > 0 and lives > 0:
# letters used
# " ".join(['a', 'b', 'cd']) --> a b cd
print("You have already used: ", " ".join(sorted(used_letters)).upper())
# what the current word is (ie., W _ R D)
word_list = [letter if letter in used_letters else '-' for letter in word]
print("Current Word: ", " ".join(word_list).upper())
print("lives remaining: ", lives)
user_letter = input("Guess a letter: ").lower()
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 = lives - 1
print(f"Letter '", user_letter, "' not in word")
print("lives remaining: ", lives)
elif user_letter in used_letters:
print("You have already used that character. Please try again!")
else:
print("Invalid character. Please try again!")
# gets here when len(word_letters) == 0:
if lives == 0:
print("Sorry, you died!. The word was ", str(word).upper())
else:
word_list = [letter if letter in used_letters else '-' for letter in word]
print("Current Word: '", " ".join(word_list).upper(), "'")
print("You guessed the word '", str(word).upper(), "'!!!")
hangman()