Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
import random

def get_word():
    words = ['python', 'programming', 'computer', 'science', 'algorithm', 'data']
    return random.choice(words)

def play_game():
    word = get_word()
    word_letters = set(word)
    alphabet = set('abcdefghijklmnopqrstuvwxyz')
    used_letters = set()

    while len(word_letters) > 0:
        print('You have used these letters: ', ' '.join(used_letters))
        word_list = [letter if letter in used_letters else '-' for letter in word]
        print('Current word: ', ' '.join(word_list))

        user_letter = input('Guess a letter: ').lower()
        if user_letter in alphabet - used_letters:
            used_letters.add(user_letter)
            if user_letter in word_letters:
                word_letters.remove(user_letter)
            
            else:
                print('Letter is not in word.')
        
        elif user_letter in used_letters:
            print('You have already used that letter. Guess another letter.')
        
        else:
            print('Invalid character. Please try again.')

    print('You guessed the word ', word, '! Congrats!')

if __name__ == '__main__':
    play_game()