Hi guys!,
I thought i'd try and learn a bit of python to keep myself occupied, and i was wondering if you could give me some constructive criticism on a word guessing game i have created. I'm an amatuer programmer and i'm always looking at improving when and where ever i can.
You can see the code below.
import random
#global
LIVES = 5
CORRECT = 0
GUESSED = []
#main is first function to be called, which intiates everything.
def main():
startGame()
difficulty = input()
playGame(difficulty)
#choice for options
def startGame():
print ("So you would like to play a word game? \n")
print ("Please choose a difficulty")
print ("0: Easy")
print ("1: Medium")
print ("2: Hard")
#which actually initiates the game fully, bringing in the right difficulty, then running the game.
def playGame(difficulty):
global LIVES
global CORRECT
global GUESSED
if(difficulty == 0):
selectedWord = ["dog", "cat", "cow", "eat", "mut","die","cry","buy","why"]
elif(difficulty == 1):
selectedWord = ["joker","poker","pride","cried","lied"]
elif(difficulty == 2):
selectedWord = ["medium", "elephant", "cheeses", "breeder", "sugar", "python"]
print("Easy? Really? You Wimp!\n")
length =random.choice(selectedWord)
print("The length of your word is"), len(length)
print length #in for testing purposes.
while(LIVES !=0 and CORRECT != 1):
word_update(length,GUESSED)
if(CORRECT==1):
break
print("Press 1 to enter a letter, or 2 to guess the full word")
enternw = input()
guess(enternw, length)
replayGame(length)
#replay the game option.
def replayGame(word):
global LIVES
if(LIVES == 0):
print ("GAME OVER, You ran out of LIVES \n The word was"), word
print ("Would you like to replay?, Y for yes, N for no")
replay = raw_input()
if(replay == "y" or replay == "Y"):
LIVES = 5
main()
elif(replay == "n" or replay == "N"):
print ("Thank you for playing")
else:("Unknown Option. Goodbye")
#determines whether to enter letter or word, then runs and controls lives etc.
def guess(enternw, length):
global LIVES
global CORRECT
global GUESSED
if(enternw == 1):
print("You chose to select a letter, please selecter a letter")
letter = raw_input()
if (letter in GUESSED):
print("You already guessed"), letter
elif(letter in length):
print"Found",letter
print length.find(letter)
GUESSED.append(letter)
else:
print "Not Found"
LIVES = LIVES - 1
print LIVES,("Lives Remain")
GUESSED.append(letter)
if(enternw == 2):
print("You chose to guess a word, please guess the word.")
guessedWord = raw_input()
if(guessedWord == length):
print("Correct, the word was"), length
CORRECT = 1
else:
print("Sorry, wrong word, keep trying!")
LIVES = LIVES - 1
print LIVES,("Lives Remain")
#courtesy of http://www.daniweb.com/software-development/python/threads/59737/guess-a-word
def word_update(word, letters_guessed):
global CORRECT
masked_word = ""
for letter in word:
if letter in letters_guessed:
masked_word += letter
else: masked_word += "-"
print "The word:", masked_word
if(word == masked_word):
print("You guessed correct, well done!")
CORRECT = 1
main()