Back to SDS/2 Parametric Scripts

 

# hangman.py

from time import sleep

from random import sample

 

def hangman():

    print '--------'

    print '|______|'

    print ' O '

    print ' O '

    print ' O '

    print ' O '

    print ' O '

    print '----'

    print '|___|'

 

def hangman1(hint_str, letter, pos_list):

    hangman()

    for i in pos_list:

        lst = [hint_str[:i], ]

        if len(hint_str) >= i+1:

            lst.append(hint_str[(i+1):])

        hint_str = letter.join(lst)

    print 'Hint: %s' % (hint_str)

    return hint_str

 

def get_index(s, item, i=0):

    i_list = []

    while True:

        try:

            i = s.index(item, i)

            i_list.append(i)

            i += 1

        except:

            break

    return i_list

 

def play(word):

    guess_list = []

    hint_string = '_' * len(word)

 

    hangman()

    print 'Hint: %s' % (hint_string)   

   

    while True:

        letter = raw_input("Enter a letter or '?' if you are ready to solve the word.")

        if letter == '?':

            hint_string = raw_input("Give it your best shot!")

            if hint_string == word:

                print "You entered the word '%s'" % (hint_string)

                print "Congratulations! You have solved the word!"

                break

            else:

                print "You have failed miserably!"

                break

        guess_list.append(letter)

        guess_list.sort()

        print 'So far you have guessed the following letters: %s' % (','.join(guess_list))

        index_list = get_index(word, letter)

        if len(index_list) > 0:

 

            if len(index_list) == 1:

                s, t = 'was', 'occurrence'

            else:

                s, t = 'were', 'occurrences'

               

            if letter in hint_string:

                print "You have already guessed letter '%s' dodo!" % (letter)

                hint_string = hangman1(hint_string, letter, [])

            else:

                print "There %s %s %s of '%s' in the word." % (s, len(index_list), t, letter)

                hint_string = hangman1(hint_string, letter, get_index(word, letter))

        else:

            print "There were no occurrences of '%s' in the word." % (letter)

            hint_string = hangman1(hint_string, letter, [])

 

        if hint_string == word:

            print "Congratulations! You have solved the word!"

            break

    return None

       

c = raw_input('Would you like to play hangman? Type y or n ')

word = sample(('soccer', 'robbery', 'antibiotics', 'python', 'parametric', 'Mississippi', 'Tennessee'), 1)[0]

 

if c == 'y':

    sleep(2)

    print "Alright, let's play "

    play(word)

 

else:

    sleep(2)

    print 'Ok, Buh Bye then'

 

"""

>>> Alright, let's play

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: _______

So far you have guessed the following letters: p

There were no occurrences of p in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: _______

So far you have guessed the following letters: p,r

There were 2 occurrences of r in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: r____r_

So far you have guessed the following letters: p,r,y

There was 1 occurrence of y in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: r____ry

So far you have guessed the following letters: o,p,r,y

There was 1 occurrence of o in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: ro___ry

So far you have guessed the following letters: b,o,p,r,y

There were 2 occurrences of b in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: robb_ry

So far you have guessed the following letters: b,e,o,p,r,y

There was 1 occurrence of e in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: robbery

Congratulations! You have solved the word!

>>>

"""

"""

Alright, let's play

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: ______

So far you have guessed the following letters: s

There were no occurrences of s in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: ______

So far you have guessed the following letters: p,s

There was 1 occurrence of p in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: p_____

You entered the word 'python'

Congratulations! You have solved the word!

"""

"""

>>> Alright, let's play

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: ___________

So far you have guessed the following letters: a

There were no occurrences of a in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: ___________

So far you have guessed the following letters: T,a

There were no occurrences of T in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: ___________

So far you have guessed the following letters: M,T,a

There was 1 occurrence of M in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: M__________

So far you have guessed the following letters: M,T,a,i

There were 4 occurrences of i in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: Mi__i__i__i

You entered the word 'Mississippi'

Congratulations! You have solved the word!

>>>

"""

"""

>>> Alright, let's play

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: ______

So far you have guessed the following letters: c

There were 2 occurrences of c in the word.

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: __cc__

You entered the word 'soccer'

Congratulations! You have solved the word!

>>>

"""

"""

Hint: s____r

So far you have guessed the following letters: r,s,s,s

You have already guessed letter s dodo!

--------

|______|

 O

 O

 O

 O

 O

----

|___|

Hint: s____r

>>>

"""