I just started learning about Python at school and decided to start this little project with the basic knowledge I have. Most of the code is pretty lengthy and I know there are probably more efficient ways of doing it so Im looking for any improvements and tips that will help my code.
#Python Game - Hangman
#Imports
import random
import string
import sys
#Custom Functions
def Lives(Incorrect): #This function tells the player how many lives they have left when called
LivesLeft = IncorrectLimit - Incorrect
print("You have", LivesLeft, "lives left.")
def PicNo(Incorrect): #This function prints the hangman picture to the corresponding incorrect guesses
if Incorrect == 1:
print(Pic1)
if Incorrect == 2:
print(Pic2)
if Incorrect == 3:
print(Pic3)
if Incorrect == 4:
print(Pic4)
if Incorrect == 5:
print(Pic5)
if Incorrect == 6:
print(Pic6)
if Incorrect == 7:
print(Pic7)
def Hint(Incorrect, WordChoice): #This function prints a hint when the player gets down to their last two guesses
if Incorrect == 6:
WordChoice = list(WordChoice)
WordHint = random.choice(WordChoice)
WordChoice = "".join(WordChoice)
print("Hint - the word contains the letter: ", WordHint)
#Constants
List1 = ["fable","surf","english","paris","flower","laptop","sand","classroom", "blonde", "star", "blue", "hat", "snow", "tennis", "gate", "palm","internet","bricks", "bird", "mountain", "crate", "jumper", "whiteboard", "teacher", "table", "paving", "vehicle", "roundabout", "grass", "window", "cat", "dog", "bag", "bycicle", "jewellery", "headphones"] #These are all the words that can possibly be taken
IncorrectLimit = 7 #The number of guesses a player can get incorrect
Infinity = 9999999999999999999999999999999999 #An amount set for for loops
Pic1 = ('''
+---+
| |
|
|
|
|
=========''')
Pic2 = ('''
+---+
| |
O |
|
|
|
=========''')
Pic3 = ('''
+---+
| |
O |
| |
|
|
=========''')
Pic4 = ('''
+---+
| |
O |
/| |
|
|
=========''')
Pic5 = ('''
+---+
| |
O |
/|\ |
|
|
=========''')
Pic6 = ('''
+---+
| |
O |
/|\ |
/ |
|
=========''')
Pic7 = ('''
+---+
| |
O |
/|\ |
/ \ |
|
=========''') #The hangman pictures
#Variables
Incorrect = 0 #The incorrect count starts at 0
List2 = [] #List2 starts empty
#Start - Menu
print("PYTHON HANGMAN") #Startup title and menu
print("Coded by - -\n")
input("Press \"Enter\" to start a game." + "\n")
#GAME START
#Processing
for i in range(Infinity):
WordChoice = (random.choice(List1)) #Randomly chooses a word from List 1
LetterCount = len(WordChoice) #Returns the amount of letters in the chosen word
print("<-----[NEW GAME]----->\n")
print("The word is", LetterCount, "letters long.") #Prints the letter count of the word
if LetterCount == 1: #Prints the 'mystery' words length with underscores and spaces
L = ("_")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 2:
L = ("__")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 3:
L = ("___")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 4:
L = ("____")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 5:
L = ("_____")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 6:
L = ("______")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 7:
L = ("_______")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 8:
L = ("________")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 9:
L = ("_________")
print(L.replace("", " ")[1: -1], "\n")
if LetterCount == 10:
L = ("__________")
print(L.replace("", " ")[1: -1], "\n")
GuessList = list(L) #Sets the chosen mystery underscore string to a list
LivesLeft = IncorrectLimit - Incorrect #Calculates the lives left
for i in range(Infinity): #Loops the following indented code
if GuessList == WordChoice:
break
Hint(Incorrect, WordChoice) #The hint custom function
for i in range(Infinity): #Loops the guessing process
Guess1 = input("Guess a letter: ") #The players letter guess
GuessLength = len(Guess1)
if GuessLength > 1:
print("Please enter only one letter.") #Repeats the guessing process if there is more than one letter
elif Guess1 == "":
print("Please enter a letter.") #Repeats the guessing process if there is not input
else:
break
Guess1 = Guess1.lower() #Turns the input into a lowercase string
List2.append(Guess1) #Adds the guess to List2
if WordChoice.find(Guess1) >= 0: #A boolean that finds if the guess is contained in the word
GuessList = list(GuessList) #Sets GuessList to a list
for x, y in enumerate(WordChoice): #Replaces the underscore from GuessList with the correct corresponding letter
if Guess1 == y:
GuessList[x] = y
print("The letter", Guess1.upper(), "was correct.") #Prints a message telling the player they were correct
GuessList = "".join(GuessList) #Sets GuessList back to a string
else:
Incorrect += 1 #Increases Incorrect up 1 everytime the player is incorrect
print("Incorrect.")
GuessList = "".join(GuessList) #Sets GuessList back to a string
PicNo(Incorrect) #The PicNo custom function
Lives(Incorrect) #The Lives custom function
List2 = "".join(List2) #Sets List2 to a string
print("Guessed Letters: ",List2.upper()) #Prints the players guessed letters
List2 = list(List2) #Sets List2 back to a list
List2.append(", ") #Adds a comma and space to List2 for formatting
print(GuessList.replace("", " ")[1: -1], "\n") #Prints the GuessList with spaces
if GuessList == WordChoice: #Determines if the player has won the game
print("Well done, you guessed the correct word!")
Incorrect = 0
List2 = []
GuessList = []
NewGame = input("Would you like to play Hangman again? [Y]es, [N]o: \n")
if NewGame == "Y":
break
else:
sys.exit()
if Incorrect == 7: #Determines if the player has lost the game
print("Game over.")
print("The word was: ", WordChoice + ".")
Incorrect = 0
List2 = []
GuessList = []
NewGame = input("Would you like to play Hangman again? [Y]es, [N]o: \n")
if NewGame == "Y":
break
else:
sys.exit()