Untitled
unknown
plain_text
2 years ago
1.3 kB
5
Indexable
import random def hangman(): word_list = ['python', 'java', 'ruby', 'javascript', 'html', 'css'] # List of words to choose from chosen_word = random.choice(word_list).lower() guessed_letters = [] attempts = 6 while True: print("\n") for letter in chosen_word: if letter in guessed_letters: print(letter, end=" ") else: print("_", end=" ") print("\nAttempts remaining:", attempts) guess = input("Guess a letter: ").lower() if guess.isalpha() and len(guess) == 1: if guess in guessed_letters: print("You've already guessed that letter. Try again!") else: guessed_letters.append(guess) if guess in chosen_word: print("Correct guess!") else: attempts -= 1 print("Wrong guess!") else: print("Invalid input. Please enter a single letter.") if attempts == 0: print("You ran out of attempts! The word was:", chosen_word) break elif all(letter in guessed_letters for letter in chosen_word): print("Congratulations! You guessed the word:", chosen_word) break hangman()
Editor is loading...