I'm quite new to Python and have been experimenting. So far I have made a simple hangman type game which creates a random word and the user than guesses it. I'd like to know if I've committed any bad practises so I can learn early on what to and not-to do.
#Word Game Python from random import randint #Define an alphabet of valid letters alphabet_file = "alphabet.txt" #Assign that to a list alphabet = open(alphabet_file).read().splitlines() #Define a word #Find the dictionary word_file = "words.txt" #Assign the dictionary to a list words = open(word_file).read().splitlines() #Define word based off a random word in the dictionary word = words[randint(0,len(words))] #Make the word lower case word = word.lower() #Make a list of guesses guesses =  #Make a list of correct guesses correctGuesses =  #Make a list of incorrect guesses incorrectGuesses =  #Set a maximum number of incorrect guesses incorrectGuessesMax = 7 #A function to help check if something is a number def isNumber (n): try: float(n) return True except ValueError: return False #Returns True if a letter is in a word def isInWord (letter,word): #Make sure letter and word are both strings if not letter in alphabet: return False #Make the letter lower case letter = letter.lower() #check if the letter is in the word if letter in word: #if so return true return True else: #Otherwise return false return False #Returns how many times a letter appears in a word def timesInWord (letter,word): #Make the letter Lower Case letter = letter.lower() #Define a variable to store the times letter is found letterCount = 0 #Check if the letter is in the word if isInWord(letter,word): #Loop through word to find letter for l in range(0,len(word)): #If the letter is found if letter == word[l]: #Add 1 to letterCount letterCount += 1 #Add the guess to the list of correct guesses if not isInWord(letter,guesses): correctGuesses.append(letter) guesses.append(letter) #Return the time the letter was found return letterCount else: if not isInWord(letter,guesses): guesses.append(letter) incorrectGuesses.append(letter) return 0 #Ask the user to input a letter def getLetter (): letter = input("Please enter a letter ") #Make sure it's actually a letter(is not a number) x = letter if isNumber(x): print ("Invalid") return getLetter() #Make sure it's only one letter #If it's a word it's ok, that will be chacked later if len(letter) == 0: print("Invalid") return getLetter() #If everything is ok, return the letter return letter #A function to print the word based on correct guesses def printWord(word,guesses): #define a printout variable printout = "" #loop through each letter in the word to check it against the guesses for i in range(0,len(word)): if word[i] in guesses: #if there's a match add the letter to the printout printout += word[i] else: #otherwise fill it with an _ printout += "_" return printout #Check if word is complete def checkWord(word,guesses): if word == printWord(word,guesses): return True else: return False #While the word is not complete keep asking for an input while checkWord(word,correctGuesses) == False: #Terminate while loop if there are to many incorrect guesses if len( incorrectGuesses ) > incorrectGuessesMax: print ("Too many incorrect guesses, the word was: " + word) break #Receive an input guess = getLetter() #Chack if the input was a letter or word if len(guess) > 1: #Check if the guess is equal to the word if guess == word: #Inform the user of their success print("Well Done, You guessed the word correctly in " + str(len(guesses)) + " guesses!") #Terminate the loop break else: print(guess + " is not the word you are looking for") else: #Print how many letters are correct print(str(timesInWord(guess,word)) + " out of " + str(len(word))) #Print letter guesses print ("Your guesses are: " + str(guesses)) #Print the amount of guesses left guessesLeft = incorrectGuessesMax - len(incorrectGuesses) guessesLeft += 1 print ("You have " + str(guessesLeft) + " incorrect guesses left") #Print the current state of the word based on correct guesses print(printWord(word,correctGuesses)) #Check if the word is completed if printWord(word,correctGuesses) == word: #Inform the user of their success print("Well Done, You guessed the word correctly in " + str(len(guesses)) + " guesses!") #Terminate the loop break