Adding hints to a basic word jumble game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cjllewey369
    New Member
    • Mar 2007
    • 14

    Adding hints to a basic word jumble game

    I am working on learning Python out of a book called Python Programming for the Absolute Beginner. Right now I am working on a challenge at the end of one of the chapters. During the chapter i was walked through the production of a word jumble game. At the end I am asked to add hints for each word and add a scoring system to reward the player for each word he guesses without having to use the hint. im not really sure the best way to go about this. Any suggestions and tips would be greatly appreciated!

    here is the code for the game:
    Code:
    # Word Jumble
    #
    # The computer picks a random word then "jumbles" it
    # The player has to guess the original word
    
    import random
    
    # create a sequence of words to choose from
    WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
             "anion", "cation", "polymorphasis", "stipulation", "antecedant")
    
    # pick one word randomly from the sequence
    word = random.choice(WORDS)
    # create a variable to use later to see if the guess is correct
    correct = word
    
    # create a jumbled version of the word
    jumble =""
    
    while word:
        position = random.randrange(len(word))
        jumble += word[position]
        word = word[: position] + word[(position + 1):]
    
    # start the game
    print \
    """
                 Welcome to Word Jumble!
    
        Unscramble the letters to make a word.
    (Press the enter key at the prompt to quit.)
    """
    print "The jumble is:", jumble
    
    guess = raw_input("\nYour guess: ")
    guess = guess.lower()
    while (guess != correct) and (guess != ""):
        print "Sorry, thats not it."
        guess = raw_input("Your guess: ")
        guess = guess.lower()
    if guess == correct:
        print "That's it! You guessed it!\n"
    
    print "Thanks for playing"
    
    raw_input("\n\nPress the enter key to exit.")



    thank you for your time and help!
    Last edited by bartonc; Mar 7 '07, 04:44 PM. Reason: added [code][/code] tags
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    I've added code tags to your post so that others can see the structure of your code. You can learn how to do this by reading the "POSTING GUIDELINES" or "REPLY GUIDELINES" on the right hand side of the page (while you are posting or replying). Thanks.

    Comment

    • Cjllewey369
      New Member
      • Mar 2007
      • 14

      #3
      ok, sorry. i didn't see that earlier. I thought there might be something like that but i wasn't sure. Now i know! thanks.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by Cjllewey369
        I am working on learning Python out of a book called Python Programming for the Absolute Beginner. Right now I am working on a challenge at the end of one of the chapters. During the chapter i was walked through the production of a word jumble game. At the end I am asked to add hints for each word and add a scoring system to reward the player for each word he guesses without having to use the hint. im not really sure the best way to go about this. Any suggestions and tips would be greatly appreciated!

        here is the code for the game:
        Code:
        # Word Jumble
        #
        # The computer picks a random word then "jumbles" it
        # The player has to guess the original word
        
        import random
        
        # create a sequence of words to choose from
        WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
                 "anion", "cation", "polymorphasis", "stipulation", "antecedant")
        
        # pick one word randomly from the sequence
        word = random.choice(WORDS)
        # create a variable to use later to see if the guess is correct
        correct = word
        
        # create a jumbled version of the word
        jumble =""
        
        while word:
            position = random.randrange(len(word))
            jumble += word[position]
            word = word[: position] + word[(position + 1):]
        
        # start the game
        print \
        """
                     Welcome to Word Jumble!
        
            Unscramble the letters to make a word.
        (Press the enter key at the prompt to quit.)
        """
        print "The jumble is:", jumble
        
        guess = raw_input("\nYour guess: ")
        guess = guess.lower()
        while (guess != correct) and (guess != ""):
            print "Sorry, thats not it."
            guess = raw_input("Your guess: ")
            guess = guess.lower()
        if guess == correct:
            print "That's it! You guessed it!\n"
        
        print "Thanks for playing"
        
        raw_input("\n\nPress the enter key to exit.")



        thank you for your time and help!
        Start your hint string with a series of underscores of length that matches the length of the word. Prompt the user to enter a question mark or whatever for a hint. As a hint, randomly select a letter to display in the correct position and replace the underscore at that position. Deduct a point or points for each hint that the user requires. Something like this:
        Code:
        The jumble is: tufdilcfi
        Hint string: _________
        Hint string: ___f_____
        Hint string: ___f____t
        HTH :)

        Comment

        • Cjllewey369
          New Member
          • Mar 2007
          • 14

          #5
          Thank you for the suggestion. Sorry it took so long for me to get back to this. That sounds like a good idea. I'm not sure how to do that though. How would I integrate that process into the existing code and attach those hints to the words? This probably seems really basic to you, but i guess I'm really new to this. Thanks for everything though!

          Comment

          • dshimer
            Recognized Expert New Member
            • Dec 2006
            • 136

            #6
            Originally posted by Cjllewey369
            Thank you for the suggestion. Sorry it took so long for me to get back to this. That sounds like a good idea. I'm not sure how to do that though. How would I integrate that process into the existing code and attach those hints to the words? This probably seems really basic to you, but i guess I'm really new to this. Thanks for everything though!
            How about testing for a character like a "?" that would indicate the user wants a hint. For example this while loop, when substituted for the original one would print the correct answer if the user enters "?". Just write a function that gives the hint in whatever form you prefer and substitute it for the "print correct" statement.
            Code:
            while (guess != correct) and (guess != ""):
            	if guess =='?':
            		print correct
            		guess = raw_input("Your guess: ")
            		guess = guess.lower()
            	else:
            		print "Sorry, thats not it."
            		guess = raw_input("Your guess: ")
            		guess = guess.lower()

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by Cjllewey369
              Thank you for the suggestion. Sorry it took so long for me to get back to this. That sounds like a good idea. I'm not sure how to do that though. How would I integrate that process into the existing code and attach those hints to the words? This probably seems really basic to you, but i guess I'm really new to this. Thanks for everything though!
              This is untested so it may not work, but maybe you will get the idea:
              Code:
              guess = ""
              lst = range(len(jumble))
              hint_str = '_'*len(jumble)
              while True:
                  guess = raw_input("Guess or '?' or 'X': ").lower()
                  if guess == correct:
                      print "That's it! You guessed it!\n"
                      break
                  elif guess == '?':
                      i = random.choice(lst)
                      lst.remove(i)
                      hint_str[i] = guess[i]
                      print hint_str
                  elif guess == 'x':
                      print "Sorry you gave up!"
                      break
                  else:
                      print "Sorry, thats not it. Try again."
              
              print "Thanks for playing"
              
              raw_input("\n\nPress the enter key to exit.")

              Comment

              • Cjllewey369
                New Member
                • Mar 2007
                • 14

                #8
                Originally posted by bvdet
                This is untested so it may not work, but maybe you will get the idea:
                Code:
                guess = ""
                lst = range(len(jumble))
                hint_str = '_'*len(jumble)
                while True:
                    guess = raw_input("Guess or '?' or 'X': ").lower()
                    if guess == correct:
                        print "That's it! You guessed it!\n"
                        break
                    elif guess == '?':
                        i = random.choice(lst)
                        lst.remove(i)
                        hint_str[i] = guess[i]
                        print hint_str
                    elif guess == 'x':
                        print "Sorry you gave up!"
                        break
                    else:
                        print "Sorry, thats not it. Try again."
                
                print "Thanks for playing"
                
                raw_input("\n\nPress the enter key to exit.")

                i'll take a look at that. it does have a few things i havnt been introduced to yet, such as "remove" and "random.choice. " it looks good though, thank you! and thank you also to dshimer.

                Comment

                • Cjllewey369
                  New Member
                  • Mar 2007
                  • 14

                  #9
                  Hey everybody. I'm back from spring break and i decided that i should finish this. So i took at look at the suggestions you all made, they were good, except part didnt quite work, and with a few tweaks, this is what i came up with:

                  Code:
                  # Word Jumble
                  #
                  # The computer picks a random word then "jumbles" it
                  # The player has to guess the original word
                  
                  import random
                  
                  # create a sequence of words to choose from
                  WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
                           "anion", "cation", "polymorphasis", "stipulation", "antecedant")
                  
                  # pick one word randomly from the sequence
                  word = random.choice(WORDS)
                  # create a variable to use later to see if the guess is correct
                  correct = word
                  
                  # create a jumbled version of the word
                  jumble =""
                  
                  while word:
                      position = random.randrange(len(word))
                      jumble += word[position]
                      word = word[:position] + word[(position + 1):]
                  
                  # sets score to zero
                  score = 0
                  
                  # start the game
                  print \
                  """
                               Welcome to Word Jumble!
                  
                      Unscramble the letters to make a word.
                  Enter a guess, an X to give up, or type ? and press enter for a hint.
                  (Press the enter key at the prompt to quit.)
                  
                  Try to get the lowest score possible. For each hint, you gain a point.
                  See if you can win with no points!
                  """
                  print jumble
                  guess = ""
                  lst = range(len(jumble))
                  hint_str = '_'*len(jumble)
                  while True:
                      guess = raw_input("Guess or '?' or 'X': ").lower()
                      if guess == correct:
                          print "That's it! You guessed it!\n Your score is", score
                          break
                      elif guess == '?':
                          i = random.choice(lst)
                          print correct[i], "is the", i+1, "letter."
                          score += 1
                      elif guess == 'x':
                          print "Sorry you gave up!"
                          break
                      else:
                          print "Sorry, thats not it. Try again."
                  
                  print "Thanks for playing"
                  
                  
                  raw_input("\n\nPress the enter key to exit.")
                  Thank you for the help!

                  Comment

                  Working...