hangman with strings and list methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deatheater1900
    New Member
    • Mar 2007
    • 2

    #16
    can i get the script for hang man as i tryed to understand but faild and try and understand it as i find it easyer
    thanks

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #17
      Originally posted by deatheater1900
      can i get the script for hang man as i tryed to understand but faild and try and understand it as i find it easyer
      thanks
      The original poster (ilikepython) didn't post his finished work and hasn't checked in since the 25th of February. But he may check in and see this. We can hope.
      Or you can start your own project and start your own thread and get help with it here on the Python Forum. Welcome to TheScripts.com.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #18
        Originally posted by bartonc
        The original poster (ilikepython) didn't post his finished work and hasn't checked in since the 25th of February. But he may check in and see this. We can hope.
        Or you can start your own project and start your own thread and get help with it here on the Python Forum. Welcome to TheScripts.com.
        sorry guys i didnt have that much time so here it is:
        Code:
        import time
        from time import *
        from random import sample
        from string import lowercase as letters
        
        playlist = []
        maxGuesses = 6
        wordList = []
        wordlist = file("totalwordlist.txt","r")
        for line in wordlist.readlines():
            line = line[:-1]
            wordList.append(line.lower())
        
        def hangman():
            print ' ' * 30, '-------- '
            print ' ' * 30, '|______| '
            print ' ' * 30, ' O '
            print ' ' * 30, ' O '
            print ' ' * 30, ' O ' 
            print ' ' * 30, ' O '
            print ' ' * 30, ' O '
            print ' ' * 30, '---- '
            print ' ' * 30, '|___| '
        def occurrences(x,z):
            count = 0
            index = x.find(z)
            while index != -1:
                count += 1
                start = index + 1
                index = x.find(z, start)
            print "There was %d occurences of the letter %s in the word" % (count, letter)
            print '\a' * count
        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 hangman1(j, letter, pos_list): 
            hangman2(charCount)
            global base
            j = base
            newbase = base[:]
            for i in pos_list:
                lst = [newbase[:i], ]
                if len(newbase) >= i+1:
                    lst.append(newbase[(i+1):])
                newbase = letter.join(lst)    
            base = newbase
            print ' ' * 30, newbase
            print '\n'
            return base
        def hangman2(x):
            if x == 0:
                hangman()
            if x == 1:
                print ' ' * 30, '-------- '
                print ' ' * 30, '|______| '
                print ' ' * 30, ' O   o'
                print ' ' * 30, ' O '
                print ' ' * 30, ' O ' 
                print ' ' * 30, ' O '
                print ' ' * 30, ' O '
                print ' ' * 30, '---- '
                print ' ' * 30, '|___| '
            if x == 2:
                print ' ' * 30, '-------- '
                print ' ' * 30, '|______| '
                print ' ' * 30, ' O   o'
                print ' ' * 30, ' O   |'
                print ' ' * 30, ' O ' 
                print ' ' * 30, ' O '
                print ' ' * 30, ' O '
                print ' ' * 30, '---- '
                print ' ' * 30, '|___| '
            if x == 3:
                print ' ' * 30, '-------- '
                print ' ' * 30, '|______| '
                print ' ' * 30, ' O   o'
                print ' ' * 30, " O  /|"
                print ' ' * 30, ' O ' 
                print ' ' * 30, ' O '
                print ' ' * 30, ' O '
                print ' ' * 30, '---- '
                print ' ' * 30, '|___| '
            if x == 4:
                print ' ' * 30, '-------- '
                print ' ' * 30, '|______| '
                print ' ' * 30, ' O   o'
                print ' ' * 30, " O  /|\\"
                print ' ' * 30, ' O ' 
                print ' ' * 30, ' O '
                print ' ' * 30, ' O '
                print ' ' * 30, '---- '
                print ' ' * 30, '|___| '
            if x == 5:
                print ' ' * 30, '-------- '
                print ' ' * 30, '|______| '
                print ' ' * 30, ' O   o'
                print ' ' * 30, " O  /|\\"
                print ' ' * 30, " O  /" 
                print ' ' * 30, ' O '
                print ' ' * 30, ' O '
                print ' ' * 30, '---- '
                print ' ' * 30, '|___| '
            if x == 6:
                print ' ' * 30, '-------- '
                print ' ' * 30, '|______| '
                print ' ' * 30, ' O   o'
                print ' ' * 30, " O  /|\\"
                print ' ' * 30, " O  / \\" 
                print ' ' * 30, ' O '
                print ' ' * 30, ' O '
                print ' ' * 30, '---- '
                print ' ' * 30, '|___| '
        def statetest():
            global state
            if len(guesslist) == 1:
                state = 'one letter'
            else:
                if len(guesslist) > 1:
                    state = 'the following letters'
        def search_win():
            if base.find('_') == -1:
                global charCount
                print 'Congratulations!! You have won!!!!!!!!!!'
                print '\a' * 3
                timed = time()
                finaltime = timed - timer
                print 'It took you %d seconds to complete the word' % (finaltime)
                playlist.append("win")
                global play
                play = False
            else:
                pass
        def losecheck():
            global play
            if charCount >= maxGuesses:
                print 'You have failed to guess the word!!'  
                print "The word was '%s'" % (word)
                playlist.append("lose")
                play = False
            else:
                pass
        
        def playnum():
            global times
            if len(playlist) == 0:
                times = 'hangman'
            else:
                if len(playlist) > 0:
                    times = 'again' 
            return times
        
        
        while True:
            play = True
            charCount = 0
            guesslist = []
            word = sample(wordList, 1)[0]
            word = str(word)  
            gameword = ' '.join(word)
            base = '_ ' * len(word)
            playnum()
        
            print 'Would you like to play %s? ' % times
            c = inputStr = raw_input()
                 
                
            if c == 'yes':    
                print "Alright, let's play"
                print '\n'
                hangman2(charCount)
                print '\n' 
                print ' ' * 30, '_ ' * len(word)
                print '\n'
                print '\a' * 2
                timer = time() 
                    
            else:
                if c == 'no':
                    print 'Ok, Buh Bye then'
                    sleep(15)
                    exit()
                    
                    
                    
                else:
                    print '\n'
                    print 'Please only enter in yes or no'
                    print '\n'
                    continue
            alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
            
            while play:            
                letter = inputStr = raw_input("Pick a letter or enter 'guess' to solve the word ")
                if letter in guesslist:
                    print '\n'
                    print 'You have already guessed that letter'
                    print '\n'
                    print 'You have guessed %s so far: %s' % (state, ', '.join(guesslist))
                    print '\n'
                    continue    
                if letter in alphabet:            
                    guesslist.append(letter)
                    if letter in word:
                        occurrences(word, letter)
                        pos = get_index (gameword, letter)                         
                        hangman1(base, letter, pos)
                        search_win()
                        
                    else:
                        if letter not in word:
                            statetest()
                            print '\n'
                            print 'There was no %s in the word' % (letter)
                            print '\n'
                            print 'You have guessed %s so far: %s' % (state, ', '.join(guesslist)) 
                            print '\n'               
                            charCount += 1
                            print '\n'
                            hangman2(charCount)
                            print '\n'
                            print ' ' * 30, base
                            print '\n'
                            losecheck()
                elif letter not in alphabet:        
                    if letter == 'guess':
                        print '\n'
                        guess = inputStr = raw_input("Ok, so guess then ")
                        if guess == word:
                            print 'Congratulations!! You have won!!!!!!!!!!'
                            print '\a' * 3
                            timed = time()
                            finaltime = timed - timer
                            print '\n'
                            print 'It took you %d seconds to complete the word' % (finaltime)
                            print '\n'
                            playlist.append("win")
                            play = False
                        elif guess != word:                
                            print 'You have failed to guess the word!!'
                            print '\n'
                            print "The word was '%s'" % (word)
                            print '\n'
                            playlist.append("lose")
                            play = False
                    print '\n'
                    print 'Please enter in a letter from the alphabet'
                    print '\n'
                        
                
                
                    
                 
                        
            
                            
                        
                
        
        sleep(20)
        exit()
        uhhh yea its really kind of messy but as soon as it worked the way i wanted it to I started working on something else so...

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #19
          Thanks, ilikepython. It's very nice of you to drop in once in a while.

          Comment

          Working...