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()

Line 62 should be
else: print("Unknown Option. Goodbye")
Also put in a console wait line after that line ...
raw_input("Press Enter to go on ...")

Line 36 should be part of the line 29 if statment.
Your print statements are misleading. For instance,
line 38 is a dangerous mix of Python2 print and Python3 print().

In general, if and elif are statements and shouldn't be written like they are functions().
In Python something() indicates a function and its call, anything else can be confusing.

Cool, i'll get these sorted.

for line 38, would
print("The length of your word is %s") % len(length)
be better?

I think a program to represent a game is a good introduction to object oriented programming. When you find yourself defining several global variables that are shared between a bunch of functions, you should build a game class. Imagine you wanted to increase the complexity of your game. At some point, you will have to reuse functions and add more to them, at each stage, the value of youra various global variables will be important to how the game proceeds... this has object oriented written all over it.

this would still be a mixed up statement and function style
print("The length of your word is %s") % len(length)
should be (Python2 only statement style)
print "The length of your word is %s" % len(length)
or better use the print() function (okay with Python2 and Python3)
print("The length of your word is %s" % len(length))

Using OOP for simple code like this is probably overkill, but might be a good exercise.
Well, at least you made the global names capitals.

Here is an example how to write your program in OOP style. Look no global stuff!
Study the details.

''' class_word_game1.py
guess the word game using Object Oriented Programming (OOP)
'''

import random

# avoids using raw_input() for strings
# code can be used with Python2 or Python3
try:
    input = raw_input
except:
    pass

class WordGame(object):

    def __init__(self):
        # prefix self. makes variable available for all methods in class
        self.lives = 5
        self.correct = 0
        self.guessed = []
        # class methods are called with a self. prefix
        self.difficulty = self.start_game()
        self.play_game()

    def start_game(self):
        '''a class method's first argument is self'''
        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")
        diffculty = int(input("Enter 0, 1 or 3: "))
        return diffculty

    def play_game(self):

        if self.difficulty == 0:
            selectedWord = ["dog", "cat", "cow", "eat",
                "mut","die","cry","buy","why"]
            print("Easy? Really? You Wimp!\n")
        elif self.difficulty == 1:
            selectedWord = ["joker","poker","pride","cried","lied"]
        elif self.difficulty == 2:
            selectedWord = ["medium", "elephant", "cheeses",
                "breeder", "sugar", "python"]

        word = random.choice(selectedWord)
        print("The length of your word is %d" % len(word))

        print(word) #in for testing purposes.
        while self.lives !=0 and self.correct != 1:
            self.word_update(word)
            if self.correct == 1:
                break
            print("Press 1 to enter a letter, or 2 to guess the full word")
            enternw = int(input(">> "))
            self.guess(enternw, word)
            self.replay_game(word)

    def replay_game(self, word):

        if(self.lives == 0):
            print("GAME OVER, You ran out of Lives!")
            print("The word was %s" % word)
            print("Would you like to replay?")
            replay = input("Y for yes, N for no ").upper()
            if replay == "Y":
                self.lives = 5
                self.play_game()
            elif replay == "N":
                print("Thank you for playing")
            else:
                print("Unknown Option. Goodbye")

    def guess(self, enternw, word):

        if enternw == 1:
            print("You chose to select a letter, please select a letter")
            letter = input(">> ")

            if letter in self.guessed:
                print("You already guessed %s" % letter)
            elif letter in word:
                print("Found %s" % letter)
                print("%d Lives Remain" % self.lives)
                #print(word.find(letter))  # test
                self.guessed.append(letter)
            else:
                print("Not Found")
                self.lives -= 1
                print("%d Lives Remain" % self.lives)
                self.guessed.append(letter)
        if enternw == 2:
            print("You chose to guess a word, please guess the word.")
            guessedWord = input()
            if guessedWord == word:
                print("Correct, the word was %s" % word)
                self.correct = 1
            else:
                print("Sorry, wrong word, keep trying!")
                self.lives -= 1
                print("%d Lives Remain" % self.lives)

    #courtesy of
    #http://www.daniweb.com/software-development/python/threads/59737/guess-a-word
    def word_update(self, word):

        masked_word = ""
        for letter in word:
            #print(letter, self.guessed)  # test
            if letter in self.guessed:
                masked_word += letter
            else:
                masked_word += "-"
        print("The word: %s" % masked_word)
        if word == masked_word:
            print("You guessed correct, well done!")
            self.correct = 1


# create the instance to start the game
mygame = WordGame()

Cheers, i don't really know why i didn't start with OO in the first place.

How do you clear the command line screen?
(i'm using work computer to do this, and we are not allowed to download to computer so i'm using repl.it to run my code.)

If you have the Windows OS, maybe you should look into Portable Python and run your code from a USB flashcard, and also use the PyScripter IDE that comes with it.

For the free download and info see:
http://www.portablepython.com/releases/

Since you are a beginner I would download Python version 2.7.3 it makes working with older code examples much easier.

The console screen can be cleared with multiple print():

for x in range(40):
    print('')

Or simply:
print('\n'*40)

Good idea, i'll look into that, and thanks for the console clear tip.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.