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

Hangman from a beginner

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

Problem

I'm trying to learn programming with python by myself and I've made a hangman game for exercise purposes.

```
import random

words = ['egypt', 'automobile', 'world', 'python', 'miniority', 'treehouse', 'friend', 'program' , 'people']

def start():
print "Hello! Welcome to a tiny hangman game that I made for exercise purposes."
raw_input('Press enter to continue')

key = random.randint(0,(len(words)-1))

global word
word = words[key]

global word_template
word_template = '_' * len(word)
word_template = list(word_template)

guesser()

def draw(int):
if int == 0:
print " _________ "
print "| | "
print "| "
print "| "
print "| "
print "| "
print "| "
elif int == 1:
print " _________ "
print "| | "
print "| 0 "
print "| "
print "| "
print "| "
print "| "
elif int == 2:
print " _________ "
print "| | "
print "| 0 "
print "| | "
print "| "
print "| "
print "| "
elif int == 3:
print " _________ "
print "| | "
print "| 0 "
print "| /| "
print "| "
print "| "
print "| "
elif int == 4:
print " _________ "
print "| | "
print "| 0 "
print "| /|\\ "
print "| "
print "| "
print "| "
elif int == 5:
print " _________ "
print "| | "
print "| 0 "
print "| /|\\ "
print "| / "
print

Solution

That's so awesome you are learning Python! I love it and I am sure you will too.

Your code looks pretty good! I played it and enjoyed doing so. It's harder than I expected though!

So here are a few quick ideas for how you could improve your code.

Fewer print statements with """

Instead of printing each line separately to create the hangman, you could use triple quotation marks to print it as a block. For example, instead of this:

print " _________     "
print "|         |    "
print "|         0    "
print "|        /|\\  "
print "|        / \\  "
print "|              "
print "|              "


...you can try this:

print """
 _________     
|         |    
|         0    
|        /|\\  
|        / \\  
|              
|              
"""


Add a space for readability

This is a minor issue, but it helps if you add a space in your string that is being printed for the user input. So instead of this:

choice = raw_input('Type retry for restart, Ctrl + C for exit:')


I would recommend this (note the space after the colon):

choice = raw_input('Type retry for restart, Ctrl + C for exit: ')


Also note that you don't have to do this for print statements where you've added an argument, because with a line of code like print 'The word was:', word Python will insert automatically insert a space between the two strings.

Wrapping more with start() aka main()

One last thing, and this is a more advanced concept. You did a good job by wrapping most of your code with the start() function. In most code I see it is more conventional to call this main() and it also will make more sense when you see the main in other languages as well. But whether you call it main() or start(), I would add your words list inside of it, like so:

def start():
    words = ['egypt', 'automobile', 'world', 
             'python', 'miniority',   # Isn't this spelled 'minority' ?
             'treehouse', 'friend', 'program' , 'people']

    print "Hello! Welcome to a tiny hangman game that I made for exercise purposes."
    raw_input('Press enter to continue')    

    key = random.randint(0,(len(words)-1))    

    global word
    word = words[key]    

    global word_template
    word_template = '_' * len(word)
    word_template = list(word_template)    

    guesser()


It's also customary to have your main() function at the very bottom of your script, so I would move your start() function to the very end of your file, but above where you are actually executing start()

From future import print_function

Building on the answer that @janos provided, I make it a habit to add this to the top of all of my Python2.X code, so that it's easier to transition it to Python3 if and when you are ready to do so:

from __future__ import print_function


I would then try and go back through all of your print statements and change it to the print() convention that @janos described. Note that you should add this as the very first import, above import random.

Hope that helps! Great job, keep at it and happy coding!

Code Snippets

print " _________     "
print "|         |    "
print "|         0    "
print "|        /|\\  "
print "|        / \\  "
print "|              "
print "|              "
print """
 _________     
|         |    
|         0    
|        /|\\  
|        / \\  
|              
|              
"""
choice = raw_input('Type retry for restart, Ctrl + C for exit:')
choice = raw_input('Type retry for restart, Ctrl + C for exit: ')
def start():
    words = ['egypt', 'automobile', 'world', 
             'python', 'miniority',   # Isn't this spelled 'minority' ?
             'treehouse', 'friend', 'program' , 'people']

    print "Hello! Welcome to a tiny hangman game that I made for exercise purposes."
    raw_input('Press enter to continue')    

    key = random.randint(0,(len(words)-1))    

    global word
    word = words[key]    

    global word_template
    word_template = '_' * len(word)
    word_template = list(word_template)    

    guesser()

Context

StackExchange Code Review Q#127718, answer score: 8

Revisions (0)

No revisions yet.