Home > front end >  Python Beginner, Easy Question: Help me to understand what this line of code does and why it impacts
Python Beginner, Easy Question: Help me to understand what this line of code does and why it impacts

Time:02-01

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:
            used_letters.add(user_letter)
            if user_letter in word_letters:
                word_letters.remove(user_letter) 
                print('')
            
            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.")
        else:
            print("That's not a valid character. Try again.")

    if lives == 0:
        print(lives_visual[lives])
        print("You have died. The word is: ", word)
    else:
        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.

CodePudding user response:

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

https://realpython.com/python-sets/

CodePudding user response:

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 block will be executed if the user_letter failed to give True in the if part, and checks if user_letter is maybe an item in the set used_letters.

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.

  • Related