Hangman code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #16
    Originally posted by St33med
    Never mind about last. The problem is when I put aList as the element in b (as in b[aList]=str(choice)) , it tells me that aList needs to be an integer, not a list. How do I transform aList into an integer?
    I'm having a hard time following along because your variable names are not very descriptive. If b (could use a better name) is a list, why not somethingbetter List?
    If aList is a list of indexes, you need to
    Code:
    b[aList][indexintothislist] = str(choice)

    Comment

    • St33med
      New Member
      • Mar 2007
      • 21

      #17
      Alright here is the more understandable code:
      Code:
      import sys # used to exit
      def guess(count,addCount,letters):
                  choice=raw_input("Guess!(lowercase only please!)")
                  aList=indexList(word, choice)
                  print aList
                  if len(choice)>1:
                      print "Don't cheat!" # Exactly :)
                      guess(count,addCount,letters)
                  elif aList!=[-1]:
                      line[aList]=str(choice) #Problem here. aList is list when needs to be integers
                      print "Good Choice!"
                      c=''.join(line)
                      print c,"Letters missed:", letters #Tells what letters the guy has missed
                      if word==c:
                          print "You Win!" 
                          sys.exit()
                      else:
                          guess(count,addCount,letters) #repeats until win or lose
                  else:
                      if choice in letters:
                          print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
                          guess(count,addCount,letters)
                      count+=addCount
                      letters[count]=choice
                      if count==0:
                          print "O"
                          addCount=1
                      elif count==1:
                          print "O<"
                          addCount=2
                      elif count==2:
                          print "0<-"
                          addCount=3
                      elif count==3:
                          print "O<-<\n Game Over XP"
                          sys.exit()
                      print "Letters missed: ", ' '.join(letters)
                      guess(count,addCount,letters)
      
      def indexList(s, item, i=0):
          i_list = []
          while True:
              try:
                  i = s.index(item, i)
                  i_list.append(i)
                  i += 1
              except:
                  i_list.append(-1)
                  break
          return i_list
      
      if __name__=='__main__':
          word=raw_input("Type a name:")
          word=''.join(word.lower().split(' '))
          #print word #This was just for testing
          line=['_ ']*len(word)
          print ''.join(line)
          count=0 #set count
          addCount=0 #set add count
          letters=['']*4 #make an empty list
          guess(count,addCount,letters)
      As it says in line 10, I need to find out how to make aList an integer because line will not recognize the elements inside a list.
      Hmmmmm.....

      Comment

      • St33med
        New Member
        • Mar 2007
        • 21

        #18
        Whoops! What I meant to say was what do you put inside the second [], bartonc?

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #19
          Originally posted by St33med
          Alright here is the more understandable code:
          Code:
          import sys # used to exit
          def guess(count,addCount,letters):
                      choice=raw_input("Guess!(lowercase only please!)")
                      aList=indexList(word, choice)
                      print aList
                      if len(choice)>1:
                          print "Don't cheat!" # Exactly :)
                          guess(count,addCount,letters)
                      elif aList!=[-1]:
                          line[aList]=str(choice) #Problem here. aList is list when needs to be integers
                          print "Good Choice!"
                          c=''.join(line)
                          print c,"Letters missed:", letters #Tells what letters the guy has missed
                          if word==c:
                              print "You Win!" 
                              sys.exit()
                          else:
                              guess(count,addCount,letters) #repeats until win or lose
                      else:
                          if choice in letters:
                              print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
                              guess(count,addCount,letters)
                          count+=addCount
                          letters[count]=choice
                          if count==0:
                              print "O"
                              addCount=1
                          elif count==1:
                              print "O<"
                              addCount=2
                          elif count==2:
                              print "0<-"
                              addCount=3
                          elif count==3:
                              print "O<-<\n Game Over XP"
                              sys.exit()
                          print "Letters missed: ", ' '.join(letters)
                          guess(count,addCount,letters)
          
          def indexList(s, item, i=0):
              i_list = []
              while True:
                  try:
                      i = s.index(item, i)
                      i_list.append(i)
                      i += 1
                  except:
                      i_list.append(-1)
                      break
              return i_list
          
          if __name__=='__main__':
              word=raw_input("Type a name:")
              word=''.join(word.lower().split(' '))
              #print word #This was just for testing
              line=['_ ']*len(word)
              print ''.join(line)
              count=0 #set count
              addCount=0 #set add count
              letters=['']*4 #make an empty list
              guess(count,addCount,letters)
          As it says in line 10, I need to find out how to make aList an integer because line will not recognize the elements inside a list.
          Hmmmmm.....
          I'm guessing that you want something like
          Code:
                          line[aList][count]=str(choice) # index into a list of indexes

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #20
            Originally posted by St33med
            Whoops! What I meant to say was what do you put inside the second [], bartonc?
            Hah, you beat me to it (by a few seconds).

            Comment

            • St33med
              New Member
              • Mar 2007
              • 21

              #21
              Weird. [count] obviously does nothing for it still gives me a TypeError.
              Specifically, the TypeError looks like this:
              Code:
              TypeError: list indices must be integers
              I might have read this wrong.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #22
                Originally posted by St33med
                Weird. [count] obviously does nothing for it still gives me a TypeError.
                Specifically, the TypeError looks like this:
                Code:
                TypeError: list indices must be integers
                I might have read this wrong.
                Code:
                elif len(aList) > 0:
                    for i in aList:
                        line[i] = choice       # is not choice already a string?
                        ..............................................................................

                Comment

                • St33med
                  New Member
                  • Mar 2007
                  • 21

                  #23
                  Originally posted by bvdet
                  Code:
                  elif len(aList) > 0:
                      for i in aList:
                          line[i] = choice       # is not choice already a string?
                          ..............................................................................
                  Yea! That worked! Before, I had to put choice as a string for some reason. It returned some type of error. But when I put in your code without the str before choice, it worked. Huh. I might've been seeing things.

                  Anyway, thank you for helping me! And, for those of you who want to see the final code, here it is:
                  Code:
                  import sys # used to exit
                  def guess(count,addCount,letters):
                              choice=raw_input("Guess!(lowercase only please!)")
                              aList=indexList(word, choice)
                              print aList
                              if len(choice)>1:
                                  print "Don't cheat!" # Exactly :)
                                  guess(count,addCount,letters)
                              elif len(aList) > 0:
                                  for i in aList:
                                      line[i] = choice  #Problem here. aList is list when needs to be integers
                                      print "Good Choice!"
                                      c=''.join(line)
                                      print c,"Letters missed:", letters #Tells what letters the guy has missed
                                      if word==c:
                                          print "You Win!" 
                                          sys.exit()
                                      else:
                                          guess(count,addCount,letters) #repeats until win or lose
                              else:
                                  if choice in letters:
                                      print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
                                      guess(count,addCount,letters)
                                  count+=addCount
                                  letters[count]=choice
                                  if count==0:
                                      print "O"
                                      addCount=1
                                  elif count==1:
                                      print "O<"
                                      addCount=2
                                  elif count==2:
                                      print "0<-"
                                      addCount=3
                                  elif count==3:
                                      print "O<-<\n Game Over XP"
                                      sys.exit()
                                  print "Letters missed: ", ' '.join(letters)
                                  guess(count,addCount,letters)
                  
                  def indexList(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
                  
                  if __name__=='__main__':
                      word=raw_input("Type a name:")
                      word=''.join(word.lower().split(' '))
                      #print word #This was just for testing
                      line=['_ ']*len(word)
                      print ''.join(line)
                      count=0 #set count
                      addCount=0 #set add count
                      letters=['']*4 #make an empty list
                      guess(count,addCount,letters) #carries everything to guess for later use

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #24
                    Originally posted by St33med
                    Yea! That worked! Before, I had to put choice as a string for some reason. It returned some type of error. But when I put in your code without the str before choice, it worked. Huh. I might've been seeing things.

                    Anyway, thank you for helping me! And, for those of you who want to see the final code, here it is:
                    Code:
                    import sys # used to exit
                    def guess(count,addCount,letters):
                                choice=raw_input("Guess!(lowercase only please!)")
                                aList=indexList(word, choice)
                                print aList
                                if len(choice)>1:
                                    print "Don't cheat!" # Exactly :)
                                    guess(count,addCount,letters)
                                elif len(aList) > 0:
                                    for i in aList:
                                        line[i] = choice  #Problem here. aList is list when needs to be integers
                                        print "Good Choice!"
                                        c=''.join(line)
                                        print c,"Letters missed:", letters #Tells what letters the guy has missed
                                        if word==c:
                                            print "You Win!" 
                                            sys.exit()
                                        else:
                                            guess(count,addCount,letters) #repeats until win or lose
                                else:
                                    if choice in letters:
                                        print "You have already guessed this!" #so he/she doesn't repeat a stupid mistake
                                        guess(count,addCount,letters)
                                    count+=addCount
                                    letters[count]=choice
                                    if count==0:
                                        print "O"
                                        addCount=1
                                    elif count==1:
                                        print "O<"
                                        addCount=2
                                    elif count==2:
                                        print "0<-"
                                        addCount=3
                                    elif count==3:
                                        print "O<-<\n Game Over XP"
                                        sys.exit()
                                    print "Letters missed: ", ' '.join(letters)
                                    guess(count,addCount,letters)
                    
                    def indexList(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
                    
                    if __name__=='__main__':
                        word=raw_input("Type a name:")
                        word=''.join(word.lower().split(' '))
                        #print word #This was just for testing
                        line=['_ ']*len(word)
                        print ''.join(line)
                        count=0 #set count
                        addCount=0 #set add count
                        letters=['']*4 #make an empty list
                        guess(count,addCount,letters) #carries everything to guess for later use
                    You have an incorrect indentation at a critical point which prevents the script from executing properly. Try 'mississippi'. Once you make this correction, you are looking good!

                    Comment

                    • St33med
                      New Member
                      • Mar 2007
                      • 21

                      #25
                      Whoops! I indented everything after the for statement, making it run only once. Fixed that and it now runs like a charm!
                      Again, Thanks!

                      EDIT: You beat me to it. Darn! :D

                      Comment

                      Working...