HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

2-player in a Python hangman game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
playergamepythonhangman

Problem

This is a follow-up: Python Hangman Program

I'm working on adding a few features to my hangman game, and I just implemented two-player gameplay, in the form of one player chooses the word to be guessed while the other player guesses said word.

I got it to work in my program, but I feel like how I did it is a bit messy since it has largely the same code between two if statements but I'm not sure how to avoid this. You'll see it in my main function, down near the bottom of the program.

Any other thoughts on the program are appreciated.

```
#!/usr/bin/env python
# coding=utf-8
#

import sys, random, os

if sys.version_info.major 1:
input("You can't guess more than one letter at a time, silly!")
elif guess in used:
input("You already tried that letter, silly!")
elif guess in word:
for index, char in enumerate(word):
if char == guess:
blanks[index] = guess
used += guess
else: # If guess is wrong
used += guess
missed = True
return blanks, used, missed

def endgame(won, word):
print ('')
if won:
print("Congratulations, you win!")
print("You correctly guessed the word '%s'!" % word)
else:
print("Nice try! Your word was '%s'." % word)
return won

def play_again():
while True:
play_again = input("Play again? [y/n] ")
if 'y' in play_again.lower():
return True
elif 'n' in play_again.lower():
return False
else:
print("Huh?")

def game(word):
'''Play one game of Hangman for the given word.
Returns True if the player wins, False if the player loses.'''
gallows = Gallows()
blanks = create_blanks(word)
used = []

while 1 == 1:
new_page()
print(gallows.get_image())
print(' '.join(blanks))
print(' '.join(used))

guess = input("Guess a letter: ")
blanks, used, missed = check_letter(word, guess, blanks, used)

Solution

One thing I would definitely change is the return type on functions like increment_count.
You don't want to leave a situation where the returns aren't logical... ie) in most cases, all possible return values of a function should return a consistent amount of variables, and have consistent types.

def increment_count(self):
    try:
        self.wrongGuesses += 1
        self.states[self.wrongGuesses]
    except IndexError:
        return False


If this function hits an IndexError, it returns False otherwise it returns None by default. This makes code confusing and hard to maintain, seeing as most users would expect all cases to return booleans.

For example, its not standard to write lines like if gallows.increment_count() == False:, the proper style for evaluating a false boolean is:

if not gallows.increment_count():


However, in your code this will make the condition always evaluate to true, considering None evaluates as false,. ie)

>>> print not None and not False
True


So I would change the original function to something like bellow, such that you can evaluate the condition like above without worry of errors/bugs.

def increment_count(self):
    try:
        self.wrongGuesses += 1
        self.states[self.wrongGuesses]
    except IndexError:
        return False
    return True

...

Code Snippets

def increment_count(self):
    try:
        self.wrongGuesses += 1
        self.states[self.wrongGuesses]
    except IndexError:
        return False
if not gallows.increment_count():
>>> print not None and not False
True
def increment_count(self):
    try:
        self.wrongGuesses += 1
        self.states[self.wrongGuesses]
    except IndexError:
        return False
    return True

...

Context

StackExchange Code Review Q#47240, answer score: 5

Revisions (0)

No revisions yet.