This is my first game and I'm really proud of it, but I think it's horribly messy. Is there any way to improve it?
#HANGMAN
# Valid Word Checker
def valid_word(word):
ill_chars = ["!","@","#","$","%","^","&","*","(",")",",",".", " ","1","2","3","4","5","6","7","8","9","0"]
for i in word:
if i in ill_chars:
print(f"\nError: It must be a letter. No symbols, numbers or spaces allowed. \n\n '{i}' is not allowed\n")
return False
if len(word) > 12:
print("\n\nError: Word is too long. Use a word with eight characters or less.")
return False
return True
word_list = []
spaces = []
guessed_letters = []
ill_chars = ["!","@","#","$","%","^","&","*","(",")",",",".", " ","1","2","3","4","5","6","7","8","9","0"]
head =" O"
armL = " /"
torso = "|"
armR = "\\"
legL = "/"
legR = " \\"
hangman_parts = [head, armL, torso, armR, legL, legR]
hangman_progress = ["",
"|",
"\n|",
"\n|"]
hangman_final = ["|~~~~|\n"]
# Check if word is valid
wordisValid = False
while True:
word = input("\n\n\nChoose any word\n\n\n").lower()
if valid_word(word) == True:
wordisValid = True
break
else:
continue
# Add to list
for i in word:
word_list.append(i)
spaces.append("_ ")
# Main Game Loop
bad_letter = 0
while wordisValid == True:
print("".join(hangman_final))
print("\n\n\n\n")
print("".join(spaces))
print("\n\nThe word has: " + str(len(word)) + " letters.")
print("\nYou've tried the following letters: " + "\n\n" + "".join(guessed_letters) + "\n\n")
# Winning Loop
if "".join(spaces) == word:
print(f"YOU WIN! The word was: {word}")
break
# Choose Letters
player_guess = input("\n\nPlease choose a letter: \n\n\n\n").lower()
guessed_letters.append(" " + player_guess)
if player_guess in ill_chars:
print(f"\nError: It must be a letter. No symbols, numbers or spaces allowed. \n\n '{player_guess}' is not allowed\n")
elif len(player_guess) > 1:
print("\nError: You must use one letter.\n")
elif player_guess == "":
print("\nError: No input provided.\n")
# Wrong Letter
elif player_guess not in word_list:
bad_letter += 1
if bad_letter == 1:
hangman_final.append(hangman_progress[1] + head)
elif bad_letter == 2:
hangman_final.append(hangman_progress[2] + " " + torso)
elif bad_letter == 3:
hangman_final.pop(2)
hangman_final.append(hangman_progress[2] + armL + torso)
elif bad_letter == 4:
hangman_final.pop(2)
hangman_final.append(hangman_progress[2] + armL + torso + armR)
elif bad_letter == 5:
hangman_final.append(hangman_progress[3] + " " + legL)
elif bad_letter == 6:
hangman_final.pop(3)
hangman_final.append(hangman_progress[3] + " " + legL + legR)
print("\n\nThe word was: " + word)
print("\n\n\n\n\n\n\n" + "".join(hangman_final))
print(" YOU GOT HUNG ")
break
print("".join(hangman_final))
print("\n\n\n\n")
print("".join(spaces))
print("\n\nThe word has: " + str(len(word)) + " letters.")
print("\nYou've tried the following letters: " + "\n\n" + "".join(guessed_letters) + "\n\n")
print(f"\n\n\n{player_guess} is not in the word. Try again.\n\n")
# END GAME
if bad_letter == 6:
break
# Add letters guessed to a list
counter = 0
for i in word:
if player_guess == i:
spaces[counter] = player_guess
counter += 1
Even though it's working, I'm not sure that I have the right idea when it comes to making this type of project.