generated from KateMcGuane/python-essentials-template-ci
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
95 lines (72 loc) · 2.97 KB
/
run.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import random # Want to be choose a word at random
from words import words
import string
"""
*GAME LOGIC*
Credit:
https://www.youtube.com/watch?v=8ext9G7xspg&t=5795s&ab_channel=freeCodeCamp.org
Get computer to choose a random word.
Check if word is valid for game use - no spaces or dashes.
"""
def get_valid_word(words):
word = random.choice(words) # Randomly chooses a word from the list
""" Continues to iterate until no longer true. """
while '-' in word or ' ' in word:
word = random.choice(words)
return word.lower() # Limit user input to lowercase
"""
Montior correctly guessed words.
Check letter validity.
"""
def gameplay():
word = get_valid_word(words) # Variable stores valid letters
word_letters = set(word) # Creates a new set of letters from word
alphabet = set(string.ascii_lowercase)
guessed_letters = set() # What the user has guessed
lives = 9
# Getting user input
# Iterate over until conditions are met
while len(word_letters) > 0 and lives > 0:
# Letters used
# ' '.join(['a', 'b', 'cd']) --> 'a b cd'
print(f"\nYou have {lives} lives left.")
print(f"\nYou have tried: {' '.join(guessed_letters)}")
# Current word is... (ie W - R D)
# Displays guessed letters
current_word_list = [letter if letter in guessed_letters else '-' for letter in word]
print(f"\nCurrent word: {' '.join(current_word_list)}")
user_letter = input("\nGuess a letter: ").lower()
if user_letter in alphabet - guessed_letters:
guessed_letters.add(user_letter)
if user_letter in word_letters:
# word_letter decreases with each correct guess
word_letters.remove(user_letter)
else:
lives = lives - 1 # Takes away a life if wrong
print("\nThat letter is not in the word.")
elif user_letter in guessed_letters:
print("\nYou have already used that letter. Please try again.")
else:
print("\nInvalid character. Please try again.")
# While condition gets here when condition is met -- len(word_letters) == 0
if lives == 0:
print(f"\nSorry, you died! The word was {word}.")
else:
print(f"\nCongratulations! You guessed the word, {word}!")
"""
If this is a valid character in the alphabet not yet used,
add this to guessed_letters set.
If the letter guessed is in the word, remove that letter
from word_letters.
"""
if user_letter in alphabet - guessed_letters:
guessed_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
elif user_letter in guessed_letters: # Invalid guess; already tried
print("\nYou have already used that letter. Please try again.")
else:
print("\nInvalid letter. Please try again.") # Invalid input
user_input = input("Welcome. Press any key to start.\n")
print(user_input)
gameplay()