Python Beginner, Easy Question: Help me to understand what this line of code does and why it impacts elif?


I just did a YouTube tutorial to make a hangman game in Python. In the below code block, what does the first line (starting with if and ending with used_letters) of code mean in plain english? I don’t understand it/what the code is doing.

            # If it's on the alphabet and not in the used letters, add it
        if user_letter in alphabet - used_letters:
            if user_letter in word_letters:
            else: lives = lives - 1
            print(user_letter, 'Letter is not in the word.')
        elif user_letter in used_letters:
            print("You have already used that character. Try again.")
            print("That's not a valid character. Try again.")

    if lives == 0:
        print("You have died. The word is: ", word)
        print("You guessed the word! It's:", word)

What I notice about this line of code, is that if the – used_letters is not there, then the elif statement will not execute when I run the program. I also don’t understand how this line of code is impacting the elif statement.

If you want to see the full hangman program (only 57 lines) follow this link. This is exactly what I coded up in Python.

Thank you so much for your time and helping me to understand this! I truly appreciate it.

Asked By: Julie S



From the code:

    alphabet = set(string.ascii_uppercase)
    used_letters = set() # what the user has guessed

you can see that alphabet and used_letters are Python set() objects. Python set stores a list of unique values and supports operations like subtraction which results in a new set with items from the first set with eliminated items which are stored in the second set.

So alphabet - used_letters is a set with all letters not yet used.

Or said with words used by Barmar in the comment: alphabet and used_letters are sets of letters. The minus - operator is the set subtraction: it returns all the elements of the first set that are not in the second set. So alphabet - used_letters is a set with all the not yet used letters.

The if queries this resulting set checking if the user_letter is an item in the set named alphabet, but not an item in the set called used_letters. If this is the case ( True ) than the if block will be executed. If this is not not the case ( False ) the if block body code will be ignored ( skipped from execution ).

The elif will be executed only if the user_letter failed to give True in the if part. The elif part of code tests then as condition for executing the elif code block if user_letter is an item in the set used_letters. If it is, then the elif code block will be executed. If it is not, then the elif code block will be skipped and the else block will be executed instead.

I suggest you make a drawing visualizing the set elements of alphabet and used_letters to better understand what is said above and directly see yourself what is going on.

Answered By: Claudio

It basically checks if letter the user typed is already on the used letters.

The way it does this, is by first subtracting all of the used letters from the alphabet, if the used letter is in the result of this substraction, then it should be added to the "used_letters" set.

This is an operation between two sets, if you want to learn more about sets and how they work, you can read here

Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.