mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 15:37:51 -08:00
Clarify variable names and simplify some expressions
This commit is contained in:
@@ -134,7 +134,7 @@ words = ["GUM", "SIN", "FOR", "CRY", "LUG", "BYE", "FLY",
|
|||||||
|
|
||||||
def play_game(guess_target):
|
def play_game(guess_target):
|
||||||
"""Play the game"""
|
"""Play the game"""
|
||||||
guess_wrong = 0
|
wrong_guesses = 0
|
||||||
guess_progress = ["-"] * len(guess_target)
|
guess_progress = ["-"] * len(guess_target)
|
||||||
guess_list = []
|
guess_list = []
|
||||||
|
|
||||||
@@ -149,6 +149,7 @@ def play_game(guess_target):
|
|||||||
guess_letter = ""
|
guess_letter = ""
|
||||||
guess_word = ""
|
guess_word = ""
|
||||||
while guess_letter == "":
|
while guess_letter == "":
|
||||||
|
|
||||||
guess_letter = input("What is your guess? ").upper()[0]
|
guess_letter = input("What is your guess? ").upper()[0]
|
||||||
if not guess_letter.isalpha():
|
if not guess_letter.isalpha():
|
||||||
guess_letter = ""
|
guess_letter = ""
|
||||||
@@ -156,8 +157,9 @@ def play_game(guess_target):
|
|||||||
elif guess_letter in guess_list:
|
elif guess_letter in guess_list:
|
||||||
guess_letter = ""
|
guess_letter = ""
|
||||||
print("You guessed that letter before!")
|
print("You guessed that letter before!")
|
||||||
|
|
||||||
guess_list.append(guess_letter)
|
guess_list.append(guess_letter)
|
||||||
guess_count = guess_count + 1
|
guess_count += 1
|
||||||
if guess_letter in guess_target:
|
if guess_letter in guess_target:
|
||||||
indices = [i for i, letter in enumerate(guess_target) if letter == guess_letter]
|
indices = [i for i, letter in enumerate(guess_target) if letter == guess_letter]
|
||||||
for i in indices:
|
for i in indices:
|
||||||
@@ -176,36 +178,38 @@ def play_game(guess_target):
|
|||||||
print("Right!! It took you", guess_count, "guesses!")
|
print("Right!! It took you", guess_count, "guesses!")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
comment, draw_bodypart = PHASES[guess_wrong]
|
comment, draw_bodypart = PHASES[wrong_guesses]
|
||||||
|
|
||||||
print(comment)
|
print(comment)
|
||||||
draw_bodypart(gallows)
|
draw_bodypart(gallows)
|
||||||
print(gallows.render())
|
print(gallows.render())
|
||||||
|
|
||||||
guess_wrong = guess_wrong + 1
|
wrong_guesses += 1
|
||||||
print("Sorry, that letter isn't in the word.")
|
print("Sorry, that letter isn't in the word.")
|
||||||
|
|
||||||
if guess_wrong == 10:
|
if wrong_guesses == 10:
|
||||||
print("Sorry, you lose. The word was " + guess_target)
|
print("Sorry, you lose. The word was " + guess_target)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main"""
|
|
||||||
|
|
||||||
random.shuffle(words)
|
random.shuffle(words)
|
||||||
word_current = 0
|
current_word = 0
|
||||||
word_count = 49
|
word_count = len(words)
|
||||||
|
|
||||||
keep_playing = True
|
keep_playing = True
|
||||||
while keep_playing:
|
while keep_playing:
|
||||||
play_game(words[word_current])
|
|
||||||
word_current = word_current + 1
|
play_game(words[current_word])
|
||||||
if word_current >= word_count:
|
current_word += 1
|
||||||
|
|
||||||
|
if current_word == word_count:
|
||||||
print("You did all the words!!")
|
print("You did all the words!!")
|
||||||
keep_playing = False
|
keep_playing = False
|
||||||
else:
|
else:
|
||||||
keep_playing = input("Want another word? (yes or no) ").lower().startswith("y")
|
keep_playing = input("Want another word? (yes or no) ").lower().startswith("y")
|
||||||
|
|
||||||
print("It's been fun! Bye for now.")
|
print("It's been fun! Bye for now.")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user