Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13921

Created this hangman game as one of my early projects as I'm still a beginner and I ran into a minor issue after completion

$
0
0

When I run my code in the terminal in vscode, instead of there appearing dashes(-) to display the certain amount of letters in the word there aren't any and it instead asks me to type a letter. Not sure if there is a problem with the logic because there are no debugging problems.

Pretty new so i didn't know what to fix but here's the code I used:

import randomfrom words import wordsimport stringdef get_valid_word(words):    word = random.choice(words) # randomly chooses something from my list     while '-' in word or '' in word:        word = random.choice(words)    return wordsdef hangman():    word = get_valid_word(words)    word_letters = set(words)  # letters in the word    alphabet = set(string.ascii_uppercase)    used_letters = set() # what the user has guessed    while len(word_letters) > 0:        # letters used        # ''.join(['a', 'b', 'cd']) --> 'a b cd'        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: ').upper()        if user_letter in alphabet - used_letters:            used_letters.add(user_letter)        if user_letter in word_letters:            word_letters.remove(user_letter)        elif user_letter in used_letters:            print('You have already used this character! Please try again.')        else:             print('Invalid character. Please try again')user_input = input('Type something:')print(user_input)`

Viewing all articles
Browse latest Browse all 13921

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>