Skip to content

Instantly share code, notes, and snippets.

@shawnpanda
Created April 18, 2015 02:14
Show Gist options
  • Select an option

  • Save shawnpanda/da79e96cab530f3da8bd to your computer and use it in GitHub Desktop.

Select an option

Save shawnpanda/da79e96cab530f3da8bd to your computer and use it in GitHub Desktop.

Revisions

  1. shawniscool created this gist Apr 18, 2015.
    145 changes: 145 additions & 0 deletions hangman.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,145 @@
    """
    This program plays a game of hangman.
    Guess letters in the mystery word.
    You can only make 8 incorrect guesses before you lose.
    See if you can guess the word before you run out of guesses...
    Author: Xiang (Shawn) Pan
    October 28, 2013
    """
    from random import*
    from string import*

    def main():
    random_word=beginning()
    length=len(random_word)
    guess_number=[8]
    list_dash=intro(length)
    while_loop(random_word,list_dash,guess_number)



    def beginning():
    """
    This function opens the text file and reads a word
    It has no input value and has two return values
    It returns the word randomly chosen and its length
    """
    word=open("words.txt",'r').readlines()
    word=word[randrange(len(word))]
    word=word.strip()
    return word

    def intro(number):
    """
    This function has one input value and one return value
    It returns the list of dashes with the corresponding length to the word chosen
    """
    all_dash="- "*number
    print all_dash
    print "incorrect guesses left: 8"
    return all_dash


    def test(all_inputs):
    """
    This function examines if the user's input is valid.
    This function has no input value from main function and has one return value.
    The user will enter the input in this step until the input is valid
    """
    guess=raw_input("Enter a letter: ")
    while not guess.isalpha() or not len(guess)==1 or guess in all_inputs:
    if not guess.isalpha() or not len(guess)==1:
    print "invalid input"
    if guess in all_inputs:
    print "You already guessed '%s' , try again..." % (guess)
    guess=raw_input("Please reenter a letter: ")

    return guess


    def check(word,input_user,list_dash,guess_number,list_input):
    """
    This function has 5 input values and 3 return values.
    It examines if the input value is a character in the word and change the list of dash corresponding to it
    """
    #print word
    if input_user in word:
    print "good guess, %s is in the word." % (input_user)
    list_dash=yes(word,input_user,list_dash)
    else:
    print guess_number[0]
    number=guess_number[0]-1
    print "sorry there is no %s in the word." % (input_user)
    print "incorrect guesses left: %d" % (number)
    guess_number[0]=number
    print guess_number
    return list_dash




    def yes(word,guess_user,list_dash):
    """
    This function examines which dashes on the list will be replaced by the input character
    It has 3 input values and 1 return value
    It returns the list of dashes with characters filled in their corresponding space
    """
    for n in range(len(word)):
    if guess_user==word[n]:
    new_dash=list(list_dash)
    new_dash[2*n]=guess_user
    list_dash="".join(new_dash)
    return list_dash


    def while_loop(random_word,list_dash,guess_number):
    """
    It is a while loop that has 3 input values and has no return value
    """
    valid=True
    work=True
    list_input=""
    while valid and work:
    guess=test(list_input)
    list_input=list_input+guess
    list_dash=check(random_word,guess,list_dash,guess_number,list_input)
    print list_dash
    valid=check_number(guess_number)
    work=check_dash(list_dash)

    def check_number(guess_number):
    """
    This function examines if the user has put input for 8 times.
    It has 1 input value and one return value
    """

    if guess_number[0] <1:
    keep_going=False
    print "Sorry, you lost."
    else:
    keep_going=True

    return keep_going

    def check_dash(list_):
    """
    This function examines if all the dashes in the list of dashes have been filled by characters.
    It has one input value and one return value
    """
    list_=list(list_)
    if not "-" in list_:
    print "You won!!! Next time I'll win though"
    valid=False
    else:
    valid=True
    return valid


    main()