Created
February 8, 2022 12:46
-
-
Save Kamforka/8b4239a626a2adde7a0fe8c4d4a2a34e to your computer and use it in GitHub Desktop.
simple hangman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hungary | Budapest | |
Switzerland | Bern | |
Germany | Berlin | |
Austria | Vienna | |
England | London | |
Scotland | Edinborough |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# intro | |
print("Welcome to Hangman") | |
# select difficulty | |
while True: | |
print("Please choose a difficulty level:") | |
print("[1] Easy - 5 ♥") | |
print("[2] Medium - 4 ♥") | |
print("[3] Hard - 3 ♥") | |
difficulty = input() | |
# validate difficulty and set lives | |
if difficulty == "1": | |
print("You've chosen the easy level, you start with 5 ♥!") | |
lives = 5 | |
elif difficulty == "2": | |
print("You've chosen the medium level, you start with 4 ♥!") | |
lives = 4 | |
elif difficulty == "3": | |
print("You've chosen the hard level, you start with 3 ♥!") | |
lives = 3 | |
else: | |
print(f"'{difficulty}' is not a valid difficulty level!") | |
continue | |
break | |
# read in guess word pool | |
with open("hangman-words.txt", "r") as hangman_fp: | |
capitals = [] | |
for line in hangman_fp: | |
capital = line.split("|")[1].strip() | |
capitals.append(capital) | |
# randomly pick a word to guess | |
word_to_guess = random.choice(capitals) | |
# start the guess loop | |
already_guessed_letters = [" "] | |
while True: | |
# display the word to guess in masked form | |
for letter in word_to_guess: | |
if letter.lower() in already_guessed_letters: | |
print(f"{letter} ", end="") | |
else: | |
print("_ ", end="") | |
print(word_to_guess, end="") | |
print() | |
# read in and validate the guessed letter | |
guess = input("Guess a letter: ").lower() | |
if guess == "quit": | |
print("Thanks for playing! Quitting...") | |
break | |
if not guess.isalpha() or len(guess) != 1: | |
print(f"'{guess}' is not a valid guess. Please type a single letter only!") | |
continue | |
if guess in already_guessed_letters: | |
print(f"'{guess}' was already guessed before!") | |
continue | |
# check if the letter is in the word | |
if guess not in word_to_guess.lower(): | |
lives = lives - 1 | |
print(f"Wrong guess! You have {lives} ♥ left!") | |
if lives == 0: | |
print("You lose!") | |
break | |
# add the letter to the already guessed list | |
already_guessed_letters.append(guess) | |
# check if the word is guessed | |
letters_found = 0 | |
for letter in word_to_guess: | |
if letter.lower() in already_guessed_letters: | |
letters_found = letters_found + 1 | |
if len(word_to_guess) == letters_found: | |
print(f"Congratulations! You guessed '{word_to_guess}' with {lives} ♥ left!") | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment