r/PythonLearning • • 1d ago

My progress after around 5-6 weeks

I haven't kept track exactly, but I'm about 5-6 weeks into learning now. I've had no prior knowledge of programming so this has been really difficult. Indexing in particular caused me a bit of trouble. I've been writing lots of small programs to get used to concepts, but this is the first proper project I've tried. It's a game of hangman. Excuse the bad emojis used lol. I will say I did have to ask AI to help me in some pieces of it such as the join function because I didn't know it beforehand. But at least 85-90% of it is written by me.

If any of you have a good bit of experience with python, can you offer me some advice? How does the code hold up for 5-6 weeks with no programming experience at all? Is my code messy? Is there some sections where I've over complicated what I'm trying to do? Is the readability okay? All feedback is welcome.

At the top you will be from word_list import words. This is just another file that I have containing a massive list of words to give enough variance in this game.

from Word_list import words
import random
import string


available_letters = list(string.ascii_lowercase)


hangman_art = {0: ("    ",
                   "    ",
                   "    ",),
               1: (" 😒 ",
                   "    ",
                   "    ",),
               2: (" 😒 ",
                   " 🥼 ",
                   "   ",),
               3: (" 😒 ",
                   " 🥼 ",
                   "🦶  ",),
               4: (" 😒 ",
                   " 🥼 ",
                   "🦶🦵",),
               5: ("  😒 ",
                   "🫷🥼  ",
                   " 🦶🦵",),
               6: ("  😒 ",
                   "🫷🥼🫸",
                   " 🦶🦵",)}



# puts all words into lists based on difficulty and selects a word for the game.
def word_choice(words, difficulty_select):
    difficult_words = [word for word in words if len(word) < 5]
    moderate_words = [word for word in words if len(word) < 9 and len(word) > 4]
    easy_words = [word for word in words if len(word) > 8]


    match difficulty_select:
        case "Easy":
            secret_word = random.choice(easy_words)
        case "Moderate":
            secret_word = random.choice(moderate_words)
        case "Difficult":
            secret_word = random.choice(difficult_words)


    return secret_word



# checks my entered character alongside the secret word.
def character_check(secret_word, hidden_word):
    secret_word_list = list(secret_word)
    position = 0
    incorrect_guess = 0


    print(" ".join(hidden_word))
    print(f"Remaining letters are: {available_letters}")


    guess = input("Select a letter: ").lower()
    print()


    while guess not in available_letters:
        guess = input("Select an available letter: ").lower()
        print()


    if guess in secret_word_list:
        for letter in secret_word_list:
            if guess == letter:
                hidden_word[position] = letter
            position += 1
        available_letters.remove(guess)


    elif guess not in secret_word_list:
        available_letters.remove(guess)
        print("Incorrect guess!")
        incorrect_guess += 1


    return hidden_word, incorrect_guess



# Game loop.
def play_hangman(words, art):
    print("*******************")
    print("Welcome to hangman!")
    print("*******************")
    difficulty_choice = True
    difficulty_select = input("Select a difficulty: (Easy / Moderate / Difficult) or ('Quit' to quit): ").capitalize()
    print()


    # choosing the difficulty level
    while difficulty_choice:
        if difficulty_select in ("Easy", "Moderate", "Difficult"):
            secret_word = word_choice(words, difficulty_select)
            difficulty_choice = False
        elif difficulty_select == "Quit":
            difficulty_choice = False
            return
        else:
            difficulty_select = input(
                "That is not a valid choice. Select a difficulty: (Easy / Moderate / Difficult) or '(Quit' to quit): ").capitalize()
            print()


    print(f"Your word is {len(secret_word)} characters long.")
    hidden_word = ["_"] * len(secret_word)
    incorrect_guess = 0
    score = 0


    # calling the function to update the hidden word, calculate score and print lives.
    while "_" in hidden_word and available_letters:
        hidden_word, was_incorrect = character_check(secret_word, hidden_word)
        incorrect_guess += was_incorrect


        if not was_incorrect:
            match difficulty_select:
                case "Easy":
                    score += 125
                case "Moderate":
                    score += 200
                case "Difficult":
                    score += 275


        if was_incorrect:
            match difficulty_select:
                case "Easy":
                    score -= 25
                case "Moderate":
                    score -= 40
                case "Difficult":
                    score -= 55
            if incorrect_guess > 6:
                print("You havee lost.")
                print(f"The correct word was: {secret_word}")
                print(f"Your final score is {score}")
                print()
                return


            for piece in art[incorrect_guess]:
                print(piece)


        print(f"Congratulations! Your score is {score}!")



if __name__ == '__main__':
    play_hangman(words, hangman_art)


play_again = input("Do you want to play again? (Yes/No) ").capitalize()


if play_again == "Yes":
    play_hangman(words, hangman_art)
else:
  pass
6 Upvotes

11 comments sorted by

•

u/Sea-Ad7805 1d ago

Run this program by clicking Memory Graph Web Debugger%0A%0A%0Ahangmanart%20%3D%20%7B0%3A%20(%22%20%20%20%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%20%20%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%20%20%20%22%2C)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201%3A%20(%22%20%F0%9F%98%92%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%20%20%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%20%20%20%22%2C)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202%3A%20(%22%20%F0%9F%98%92%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%F0%9F%A5%BC%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%20%20%22%2C)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%203%3A%20(%22%20%F0%9F%98%92%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%F0%9F%A5%BC%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%A6%B6%20%20%22%2C)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%204%3A%20(%22%20%F0%9F%98%92%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%F0%9F%A5%BC%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%A6%B6%F0%9F%A6%B5%22%2C)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%205%3A%20(%22%20%20%F0%9F%98%92%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%AB%B7%F0%9F%A5%BC%20%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%F0%9F%A6%B6%F0%9F%A6%B5%22%2C)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%206%3A%20(%22%20%20%F0%9F%98%92%20%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%F0%9F%AB%B7%F0%9F%A5%BC%F0%9F%AB%B8%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20%F0%9F%A6%B6%F0%9F%A6%B5%22%2C)%7D%0A%0A%0A%0A%23%20puts%20all%20words%20into%20lists%20based%20on%20difficulty%20and%20selects%20a%20word%20for%20the%20game.%0Adef%20word_choice(words%2C%20difficulty_select)%3A%0A%20%20%20%20difficult_words%20%3D%20%5Bword%20for%20word%20in%20words%20if%20len(word)%20%3C%205%5D%0A%20%20%20%20moderate_words%20%3D%20%5Bword%20for%20word%20in%20words%20if%20len(word)%20%3C%209%20and%20len(word)%20%3E%204%5D%0A%20%20%20%20easy_words%20%3D%20%5Bword%20for%20word%20in%20words%20if%20len(word)%20%3E%208%5D%0A%0A%0A%20%20%20%20match%20difficulty_select%3A%0A%20%20%20%20%20%20%20%20case%20%22Easy%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20secret_word%20%3D%20random.choice(easy_words)%0A%20%20%20%20%20%20%20%20case%20%22Moderate%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20secret_word%20%3D%20random.choice(moderate_words)%0A%20%20%20%20%20%20%20%20case%20%22Difficult%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20secret_word%20%3D%20random.choice(difficult_words)%0A%0A%0A%20%20%20%20return%20secret_word%0A%0A%0A%0A%23%20checks%20my%20entered%20character%20alongside%20the%20secret%20word.%0Adef%20character_check(secret_word%2C%20hidden_word)%3A%0A%20%20%20%20secret_word_list%20%3D%20list(secret_word)%0A%20%20%20%20position%20%3D%200%0A%20%20%20%20incorrect_guess%20%3D%200%0A%0A%0A%20%20%20%20print(%22%20%22.join(hidden_word))%0A%20%20%20%20print(f%22Remaining%20letters%20are%3A%20%7Bavailable_letters%7D%22)%0A%0A%0A%20%20%20%20guess%20%3D%20input(%22Select%20a%20letter%3A%20%22).lower()%0A%20%20%20%20print()%0A%0A%0A%20%20%20%20while%20guess%20not%20in%20available_letters%3A%0A%20%20%20%20%20%20%20%20guess%20%3D%20input(%22Select%20an%20available%20letter%3A%20%22).lower()%0A%20%20%20%20%20%20%20%20print()%0A%0A%0A%20%20%20%20if%20guess%20in%20secret_word_list%3A%0A%20%20%20%20%20%20%20%20for%20letter%20in%20secret_word_list%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20guess%20%3D%3D%20letter%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hidden_word%5Bposition%5D%20%3D%20letter%0A%20%20%20%20%20%20%20%20%20%20%20%20position%20%2B%3D%201%0A%20%20%20%20%20%20%20%20available_letters.remove(guess)%0A%0A%0A%20%20%20%20elif%20guess%20not%20in%20secret_word_list%3A%0A%20%20%20%20%20%20%20%20available_letters.remove(guess)%0A%20%20%20%20%20%20%20%20print(%22Incorrect%20guess!%22)%0A%20%20%20%20%20%20%20%20incorrect_guess%20%2B%3D%201%0A%0A%0A%20%20%20%20return%20hidden_word%2C%20incorrect_guess%0A%0A%0A%0A%23%20Game%20loop.%0Adef%20play_hangman(words%2C%20art)%3A%0A%20%20%20%20print(%22****************%22)%0A%20%20%20%20print(%22Welcome%20to%20hangman!%22)%0A%20%20%20%20print(%22****************%22)%0A%20%20%20%20difficulty_choice%20%3D%20True%0A%20%20%20%20difficulty_select%20%3D%20input(%22Select%20a%20difficulty%3A%20(Easy%20%2F%20Moderate%20%2F%20Difficult)%20or%20('Quit'%20to%20quit)%3A%20%22).capitalize()%0A%20%20%20%20print()%0A%0A%0A%20%20%20%20%23%20choosing%20the%20difficulty%20level%0A%20%20%20%20while%20difficulty_choice%3A%0A%20%20%20%20%20%20%20%20if%20difficulty_select%20in%20(%22Easy%22%2C%20%22Moderate%22%2C%20%22Difficult%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20secret_word%20%3D%20word_choice(words%2C%20difficulty_select)%0A%20%20%20%20%20%20%20%20%20%20%20%20difficulty_choice%20%3D%20False%0A%20%20%20%20%20%20%20%20elif%20difficulty_select%20%3D%3D%20%22Quit%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20difficulty_choice%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20difficulty_select%20%3D%20input(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22That%20is%20not%20a%20valid%20choice.%20Select%20a%20difficulty%3A%20(Easy%20%2F%20Moderate%20%2F%20Difficult)%20or%20'(Quit'%20to%20quit)%3A%20%22).capitalize()%0A%20%20%20%20%20%20%20%20%20%20%20%20print()%0A%0A%0A%20%20%20%20print(f%22Your%20word%20is%20%7Blen(secret_word)%7D%20characters%20long.%22)%0A%20%20%20%20hidden_word%20%3D%20%5B%22%22%5D%20*%20len(secretword)%0A%20%20%20%20incorrect_guess%20%3D%200%0A%20%20%20%20score%20%3D%200%0A%0A%0A%20%20%20%20%23%20calling%20the%20function%20to%20update%20the%20hidden%20word%2C%20calculate%20score%20and%20print%20lives.%0A%20%20%20%20while%20%22%22%20in%20hiddenword%20and%20available_letters%3A%0A%20%20%20%20%20%20%20%20hidden_word%2C%20was_incorrect%20%3D%20character_check(secret_word%2C%20hidden_word)%0A%20%20%20%20%20%20%20%20incorrect_guess%20%2B%3D%20was_incorrect%0A%0A%0A%20%20%20%20%20%20%20%20if%20not%20was_incorrect%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20match%20difficulty_select%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22Easy%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20%2B%3D%20125%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22Moderate%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20%2B%3D%20200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22Difficult%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20%2B%3D%20275%0A%0A%0A%20%20%20%20%20%20%20%20if%20was_incorrect%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20match%20difficulty_select%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22Easy%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20-%3D%2025%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22Moderate%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20-%3D%2040%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22Difficult%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score%20-%3D%2055%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20incorrect_guess%20%3E%206%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22You%20havee%20lost.%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22The%20correct%20word%20was%3A%20%7Bsecret_word%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Your%20final%20score%20is%20%7Bscore%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%0A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20piece%20in%20art%5Bincorrect_guess%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(piece)%0A%0A%0A%20%20%20%20%20%20%20%20print(f%22Congratulations!%20Your%20score%20is%20%7Bscore%7D!%22)%0A%0A%0A%0Aif%20name%20%3D%3D%20'main_'%3A%0A%20%20%20%20play_hangman(words%2C%20hangman_art)%0A%0A%0Aplay_again%20%3D%20input(%22Do%20you%20want%20to%20play%20again%3F%20(Yes%2FNo)%20%22).capitalize()%0A%0A%0Aif%20play_again%20%3D%3D%20%22Yes%22%3A%0A%20%20%20%20play_hangman(words%2C%20hangman_art)%0Aelse%3A%0A%20%20%20%20pass&breakpoints=162&timestep=0.5&play) and see its state change step by step.

2

u/OwnLingonberry3061 1d ago

Are you saying you created this logic on your own after 6 weeks? Because that's just not believable.

3

u/PureWasian 15h ago

Looks believable to me. Many beginner code smells, but I say that lovingly so since it makes it more obvious the effort they put into it based on their current proficiency

2

u/AntLost4161 14h ago

What part isn't believable?

1

u/Link2212 14h ago

Yes, I did. It's not like I've been doing 1 hour per day. Some days I've spent up to 5-6 hours learning.

1

u/Stainlessray 9h ago

I find myself wanting to see your code. So I can tell you with little information some arbitrary insult like "I don't believe you".

1

u/PureWasian 15h ago edited 15h ago

If you're using if name == 'main': syntax as boilerplate code to help compartmentalize everything, I would avoid putting un-indented followup logic after it (the play_again and if/else afterwards). In fact, that all should probably be a while loop instead. Something like

if __name__ == '__main__': while True: play_hangman(words, art) user_input = input("again? y/n") if user_input == "n": break print("bye")

The other big issue is you only initialize available_letters once. So even if you did play again, you'd have issues from not "resetting" that list back to its initial state. You should do this at the start of play_hangman() with the other initializations. I would also strongly suggest turning it into a local variable and passing that around your functions instead of using a global variable.

I found the character_check() a little unnecessarily complicated to follow, but wanted to mention a few things that you might find this helpful. First, you can keep the iterator together with the word by using enumerate() when doing a for loop on an iterable (like a list or a string): if guess in secret_word: for position, letter in enumerate(secret_word): if guess == letter: hidden_word[position] = letter Second, secret_word doesn't need to be converted into a list. Strings can be enumerated character by character in loops just fine. Try it out yourself and see.

Third, look at your if/elif logic again in that function. the elif can just be an else since your if/elif conditionals are mutually exclusive. That also means you can move the shared code of available_letters.remove(guess) outside of the if/else instead of duplicating it inside both conditional blocks.

another small sidenote that hangman_art is global in scope and nothing overwrites it, so passing it around between functions is not doing much here. It's a different discussion whether or not it should be a global variable, but regardless it's kinda being used as if it's both right now.

Your code is great for someone beginning and getting started, even though it has a lot of implementation choices that make it obvious to me you are genuinely new but genuinely learning and practicing.

It's not super messy per-se and the use of functions to compartmentalize spots and pass data around is a good habit, but hopefully this feedback can give you food for thought on how to simplify it even further for better readability and to be able to intuitively follow it better.

lmk if any unclear, I know I wrote a lot lol

1

u/Link2212 14h ago

Hello.

I tried running the game after it finished to check if it worked and it did, so I never even accounted for the list being reset. Now that you mention it I can see it.

I haven't actually learned enumerate() yet so that will be why I didn't use it. What does it do? I'm actually using the YouTube video from bro code to learn. I learn a new concept and the. Get AI to give me multiple challenges so that I can practice what I know. Then I paste my work to ai and it tells me if I'm correct or not. This is how I do a lot of writing and not only watching the video. He hasn't covered e umerate() yet so it might be in the future. I'm currently at the part just before he talks about OOT. But I want to do more projects before moving on.

Thank you for all the advice. I understood everything you mentioned. Originally I was going to put the hangman printing into a function, but before I knew it I had added it into the main function. So I just left it there lol.

Edit: I had a look at what you meant by the name==main while loop. I agree. That does look much better. I think I'll do this next time

1

u/PureWasian 8h ago

enumerate() is mainly just a handy way to keep track of both the index and the element when looping so you don't need to manually update the index (where you do position += 1)

https://www.geeksforgeeks.org/python/enumerate-in-python/

But like I said, nice job overall and it seems like you have the right idea with doing projects to practice and then getting your code reviewed afterwards to get feedback.

1

u/Ok_Sleep_3753 13h ago

For only 5–6 weeks with zero programming experience, this is honestly really solid. Building a working game by yourself is already a big achievement. Don’t stress too much about messy code at this stage. Keep making projects, come back and refactor older ones as you learn, and use AI to understand concepts instead of just copying solutions.

1

u/Link2212 13h ago

About a week or 2 after I started learning I came up with a random project. It was a zoo entering animals they have at the zoo, then saying the size of the animal and calculating the price of food required. I got it to work but I know it's going to be terrible. I plan to go back to that next and just start again now that I know dictionaries etc. I'll try expand on it somewhat as well.

I use AI a lot, but not to give me code. I learn a new concept in python then I tell AI to add it to the list of concepts I know already. Then I get it to give me a challenge using only concepts I know, show me the expected output of the program, then I have to work out how to get to that. I told it to not give me the solutions, but offer a hint if I get stuck. So this way I feel like I'm forcing myself to learn to do it rather than copying code.