lab metrics

This commit is contained in:
e-maks
2024-02-19 22:57:40 +03:00
parent aec65d7dab
commit b9b05eaa83
5 changed files with 133 additions and 34 deletions
+44 -30
View File
@@ -4,13 +4,14 @@ import random
import os
# Constants
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone", "hangman",
"computer", "science", "programming", "mathematics", "player", "condition",
"reverse", "water", "board", "geeks", "keyboard", "laptop", "headphone",
"mouse", "printer", "scanner", "software", "hardware", "network", "server")
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
"hangman", "computer", "science", "programming", "mathematics",
"player", "condition", "reverse", "water", "board", "geeks",
"keyboard", "laptop", "headphone", "mouse", "printer",
"scanner", "software", "hardware", "network", "server")
HANGMAN_STAGES = (
"""
"""
-------
|/ |
|
@@ -20,7 +21,7 @@ HANGMAN_STAGES = (
|
/|\\
-----------
""","""
""", """
-------
|/ |
| O
@@ -30,7 +31,7 @@ HANGMAN_STAGES = (
|
/|\\
-----------
""","""
""", """
-------
|/ |
| O
@@ -40,7 +41,7 @@ HANGMAN_STAGES = (
|
/|\\
-----------
""","""
""", """
-------
|/ |
| O
@@ -50,7 +51,7 @@ HANGMAN_STAGES = (
|
/|\\
-----------
""","""
""", """
-------
|/ |
| O
@@ -60,7 +61,7 @@ HANGMAN_STAGES = (
|
/|\\
-----------
""","""
""", """
-------
|/ |
| O
@@ -70,7 +71,7 @@ HANGMAN_STAGES = (
|
/|\\
-----------
""","""
""", """
-------
|/ |
| O
@@ -80,7 +81,7 @@ HANGMAN_STAGES = (
|
/|\\
-----------
""","""
""", """
-------
|/ |
| O
@@ -92,19 +93,22 @@ HANGMAN_STAGES = (
-----------
""")
# Functions
def clear_screen():
"""Clears the screen."""
os.system("cls" if os.name == "nt" else "clear")
def get_random_word():
"""Returns a random word from the WORDS tuple."""
return random.choice(WORDS)
def splash_screen():
"""The splash screen."""
print("""
_ _ -----
_ _ -----
| | | | __ _ _ __ __ _ _ __ ___ __ _ _ __ | o
| |_| |/ _` | '_ \\ / _` | '_ ` _ \\ / _` | '_ \\ | /|\\
| _ | (_| | | | | (_| | | | | | | (_| | | | | | / \\
@@ -115,6 +119,28 @@ def splash_screen():
clear_screen()
def single_letter_check(guessed_letters):
while True:
guess = input("Guess a letter: ").lower()
if len(guess) == 1 and guess.isalpha():
# check if letter has already been guessed
if guess in guessed_letters:
print("You have already guessed that letter.")
else:
break
else:
print("Invalid guess. Please enter a single letter.")
return guess
def add_guess_to_word(word, guess, guessed_word):
# add guess to guessed word
for i in range(len(word)):
if word[i] == guess:
guessed_word[i] = guess
return guessed_word
def hangman():
"""The hangman game."""
try:
@@ -132,7 +158,7 @@ def hangman():
print("Hangman game. Try to guess the word. (CTRL+C to quit)")
print(HANGMAN_STAGES[wrong_guesses])
print(" ".join(guessed_word), end=' ')
print("(Guessed letters: " + ", ".join(guessed_letters),")")
print("(Guessed letters: " + ", ".join(guessed_letters), ")")
print()
# check if player has won
@@ -141,31 +167,19 @@ def hangman():
break
# check if player has lost
if wrong_guesses == len(HANGMAN_STAGES)-1:
if wrong_guesses == len(HANGMAN_STAGES) - 1:
print("You lose!")
break
# make sure player enters a single letter
while True:
guess = input("Guess a letter: ").lower()
if len(guess) == 1 and guess.isalpha():
# check if letter has already been guessed
if guess in guessed_letters:
print("You have already guessed that letter.")
else:
break
else:
print("Invalid guess. Please enter a single letter.")
guess = single_letter_check(guessed_letters)
# add guess to guessed letters
guessed_letters.append(guess)
# check if guess is in word
if guess in word:
# add guess to guessed word
for i in range(len(word)):
if word[i] == guess:
guessed_word[i] = guess
guessed_word = add_guess_to_word(word, guess, guessed_word)
else:
# increment wrong guesses
wrong_guesses += 1
@@ -180,7 +194,7 @@ def hangman():
except KeyboardInterrupt:
print("\nGoodbye!")
exit()
# Main
if __name__ == "__main__":