(Python) Why doesn't my program run anything while my CPU usage skyrockets?

Viewed 21

So I was following this tutorial on youtube however when it comes to running the program, nothing shows up on the terminal while my CPU doubles in usage. I am using VScode to run this program. PS. I am very new to this and I always have frequent trouble when it comes to coding in general so i decided to resort in asking questions.

import random
from words import words
import string

def get_valid_word(words):
    word = random.choice(words) #randomly chooses something from the list
    while '-' in word or '' in word: #helps pick words that have a dash or space in it
        word = random.choice(words)
    return word.upper() #returns in uppercase 

def hangman():
    word = get_valid_word(words)
    word_letters = set(word) # saves all the letters in the word
    alphabet = set(string.ascii_uppercase) #imports predetermined list from english dictionary
    used_letters = set() #keeps track of what user guessed
    

    #getting user input
    while len(word_letters) > 0: #keep guessing till you have no letters to guess the word
        #letters used
        #'' .join (['a', 'b', 'cd']) --> 'a, b cd'
        print('you have used these letters: ',' '.join(used_letters))
        #what the current word is (ie W - R D)
        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: ')
        if user_letter in alphabet - used_letters: #letters haven't used
            used_letters.add(user_letter) #add the used letters
            if user_letter in word_letters: #if the letter that you've just guessed is in the word then you remove that letter from word_letters. So everytime you've guessed correctly from the word letters which is keeping track of all the letters in the word decreases in size.
                word_letters.remove(user_letter)

        elif user_letter in used_letters:
            print('You have used this')
        else:
            print('Invalid')
    #gets here when len(word_letters) == 0


hangman()

0 Answers
Related