I recently started learning Python, and made a simple text based Hangman game. I wanted to know if there could be anything done to make the code cleaner or more efficient.
import random
import os
import sys
def main():
pickword()
def start(word, twoplayer):
wordarray = ''
userguess = []
guessright = 0
lettersright = 0
if len(word) < 5:
tries = 6
else:
tries = 10
usedletters = []
if twoplayer == False:
print(word)
print("Welcome to hangman")
print("I will think of a word and you will have to guess it, are you ready!")
input("Press enter to continue")
else:
print("Welcome to hangman")
print("Player 1 has already thought of a word, let's begin")
input("Press enter to continue")
lengthword = len(word)
if twoplayer == False:
for i in range(1, lengthword): #creates a new variable, all underscores
userguess.append("_ ")
else:
for i in range(1, lengthword): #creates a new variable, all underscores
userguess.append("_ ")
wordarray = (list(word))
if twoplayer == False:
for i in range(0, lengthword): #removes \n from end of word
if wordarray[i] == "\n":
wordarray.remove(wordarray[i])
break
usedletters = []
getuserguess(wordarray, userguess, tries, usedletters, word)
def pickword():
word = 0
twoplayer = input("Do you want to play 2 player[Y/N]")
if twoplayer.lower() == "y":
word = input("Player 1, enter the word you want player 2 to guess, don't let them see! ")
if len(word) < 3:
print("That word is too short")
pickword()
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
word = word.lower()
twoplayer = True
start(word, twoplayer)
else:
try:
word = random.choice(open('hangman.txt').readlines())
twoplayer = False
start(word, twoplayer)
except:
input("Error, no word list found, press enter to exit")
sys.exit()
def getuserguess(wordarray, userguess, tries, usedletters, word):
print(' '.join(userguess))
guess1 = input("Enter your guess: ")
if len(guess1) < 1 or len(guess1) > 1 or guess1.isdigit():
print("Invalid input")
getuserguess(wordarray, userguess, tries, usedletters, word)
else:
checkuserguess(wordarray, userguess, guess1, usedletters, tries, word)
def lose(word):
again = input("Sorry, you lost, the word was {}Would you like to play again[Y/N]".format(word))
if again.lower() == "y":
pickword()
else:
sys.exit()
def win():
again = input("Congrats, you won hangman, do you want to play again?[Y/N]")
if again == "Y" or "y":
pickword()
else:
sys.exit()
def checkuserguess(wordarray, userguess, guess1, usedletters, tries, word):
guess1list = (list(guess1))
if guess1list[0] in usedletters:
print("You already used that letter")
getuserguess(wordarray, userguess, tries, usedletters, word)
if guess1 in wordarray:
usedletters.append(guess1)
for i in range(0, len(wordarray)):
if wordarray[i] == guess1list[0]:
userguess[i] = guess1list[0]
usedletters.append(guess1list[0])
if "_ " not in userguess:
print(' '.join(userguess))
win()
else:
getuserguess(wordarray, userguess, tries, usedletters, word)
else:
if tries > 0:
print("Sorry, that letter is not in the word, you have ", tries, " remaining")
tries -= 1
getuserguess(wordarray, userguess, tries, usedletters, word)
else:
lose(word)
main()