Flowcharts
November 26, 2020•200 words
Python bootcamp #100DaysOfCode #myNotes
As programs become bigger or more complicated. It's good to draw a flowchart first, like this one for today's hangman project.
#begin code
import random
from hangman_art import stages, logo
from hangman_words import word_list
from replit import clear
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
already_guessed = []
end_of_game = False
lives = 6
print(logo)
#Create blanks
display = []
for _ in range(word_length):
display += "_"
while not end_of_game:
guess = input("Guess a letter: ").lower()
#clear the output between guesses.
clear()
#Check guessed letter
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display[position] = letter
#Check if user is wrong.
if guess in already_guessed:
print("You already guessed this letter.")
elif guess not in chosen_word:
print(
f"The word does not contain the letter: {guess}.")
lives -= 1
if lives == 0:
end_of_game = True
print("You lose.")
#Join all the elements in the list and turn it into a String.
print(f"{' '.join(display)}")
#Check if user has got all letters.
if "_" not in display:
end_of_game = True
print("You win.")
print(stages[lives])
already_guessed += guess
#end code