Hangman Game - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code Review (https://python-forum.io/forum-46.html) +--- Thread: Hangman Game (/thread-38240.html) |
Hangman Game - monkeydesu - Sep-21-2022 I've been teaching myself how to code for a few weeks and did a hangman game all by myself! I'm so chuffed ! Such a good feeling and I still can't believe it works! I would love some tips about things to look out for, maybe something to clean the code a little? Be gentle as I'm new and super excited this all worked out haha import random def making_a_guess(): x = 0 global update_display correct_guess = False for letter in chosen_word: if guess.lower() == chosen_word[x]: blank_list[x] = guess.lower() correct_guess = True x += 1 if correct_guess == False: print(f"There is no {guess}, sorry.") update_display += 1 x = 0 HANGMANPICS = [''' +---+ | | | | | | =========''', ''' +---+ | | O | | | | =========''', ''' +---+ | | O | | | | | =========''', ''' +---+ | | O | /| | | | =========''', ''' +---+ | | O | /|\ | | | =========''', ''' +---+ | | O | /|\ | / | | =========''', ''' +---+ | | O | /|\ | / \ | | ========='''] word_list = ["aardvark", "baboon", "camel", "jazz", "grass", "follow", "castle", "cloud"] chosen_word = list(random.choice(word_list)) blank = "" for letter in chosen_word: blank += "_" blank_list = list(blank) update_display = 0 #---------------------------------------------------------------------------------------------- print(HANGMANPICS[update_display]) guess = input(f"Welcome to hangman.\n{blank}\nMake a guess? ") making_a_guess() print(HANGMANPICS[update_display]) print(''.join(blank_list)) while update_display < 6: if blank_list == chosen_word: print("YOU WIN!") break guess = input("Make another guess? ") making_a_guess() print(HANGMANPICS[update_display]) print(''.join(blank_list)) if update_display == 6: print("GAME OVER.") RE: Hangman Game - coffeejunkie34 - Sep-28-2023 if correct_guess == False:Better: if not correct_guess:referring to line 69 and some following lines: word_list = ["aardvark", "baboon", "camel", "jazz", "grass", "follow", "castle", "cloud"]I would always write global variables in uppercase like WORD_LIST instead of word_list i would use more comments. While writing simple scripts its not really necessary, it can help a lot in the future if you just used to comment your code. I am a huge fan of purpose statements blank = "" for letter in chosen_word: blank += "_" blank_list = list(blank)you can just write: blank = "_" * len(chosen_word)If you are only coding for weeks this is pretty good btw |