Tags: absolute, adding, basic, beginner, book, challenge, game, hints, jumble, learning, programming, python, word, working

Adding hints to a basic word jumble game

On Programmer » Python

41,880 words with 8 Comments; publish: Fri, 04 Jan 2008 12:46:00 GMT; (200140.63, « »)

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: ( text )
  1. # Word Jumble
  2. #
  3. # The computer picks a random word then "jumbles" it
  4. # The player has to guess the original word
  5. import random
  6. # create a sequence of words to choose from
  7. WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
  8. "anion", "cation", "polymorphasis", "stipulation", "antecedant")
  9. # pick one word randomly from the sequence
  10. word = random.choice(WORDS)
  11. # create a variable to use later to see if the guess is correct
  12. correct = word
  13. # create a jumbled version of the word
  14. jumble =""
  15. while word:
  16. position = random.randrange(len(word))
  17. jumble += word[position]
  18. word = word[: position] + word[(position + 1):]
  19. # start the game
  20. print \
  21. """
  22. Welcome to Word Jumble!
  23. Unscramble the letters to make a word.
  24. (Press the enter key at the prompt to quit.)
  25. """
  26. print "The jumble is:", jumble
  27. guess = raw_input("\nYour guess: ")
  28. guess = guess.lower()
  29. while (guess != correct) and (guess != ""):
  30. print "Sorry, thats not it."
  31. guess = raw_input("Your guess: ")
  32. guess = guess.lower()
  33. if guess == correct:
  34. print "That's it! You guessed it!\n"
  35. print "Thanks for playing"
  36. raw_input("\n\nPress the enter key to exit.")
thank you for your time and help!

All Comments

Leave a comment...

  • 8 Comments
    • 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.
      #1; Fri, 04 Jan 2008 12:47:00 GMT
    • 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.
      #2; Fri, 04 Jan 2008 12:48:00 GMT
    • Quote:
      === Original Words ===

      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: ( text )
      1. # Word Jumble
      2. #
      3. # The computer picks a random word then "jumbles" it
      4. # The player has to guess the original word
      5. import random
      6. # create a sequence of words to choose from
      7. WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
      8. "anion", "cation", "polymorphasis", "stipulation", "antecedant")
      9. # pick one word randomly from the sequence
      10. word = random.choice(WORDS)
      11. # create a variable to use later to see if the guess is correct
      12. correct = word
      13. # create a jumbled version of the word
      14. jumble =""
      15. while word:
      16. position = random.randrange(len(word))
      17. jumble += word[position]
      18. word = word[: position] + word[(position + 1):]
      19. # start the game
      20. print \
      21. """
      22. Welcome to Word Jumble!
      23. Unscramble the letters to make a word.
      24. (Press the enter key at the prompt to quit.)
      25. """
      26. print "The jumble is:", jumble
      27. guess = raw_input("\nYour guess: ")
      28. guess = guess.lower()
      29. while (guess != correct) and (guess != ""):
      30. print "Sorry, thats not it."
      31. guess = raw_input("Your guess: ")
      32. guess = guess.lower()
      33. if guess == correct:
      34. print "That's it! You guessed it!\n"
      35. print "Thanks for playing"
      36. 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: ( text )
      1. The jumble is: tufdilcfi
      2. Hint string: _________
      3. Hint string: ___f_____
      4. Hint string: ___f____t
      HTH :)
      #3; Fri, 04 Jan 2008 12:49:00 GMT
    • 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!
      #4; Fri, 04 Jan 2008 12:50:00 GMT
    • Quote:
      === Original Words ===

      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: ( text )
      1. while (guess != correct) and (guess != ""):
      2. if guess =='?':
      3. print correct
      4. guess = raw_input("Your guess: ")
      5. guess = guess.lower()
      6. else:
      7. print "Sorry, thats not it."
      8. guess = raw_input("Your guess: ")
      9. guess = guess.lower()
      #5; Fri, 04 Jan 2008 12:51:00 GMT
    • Quote:
      === Original Words ===

      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: ( text )
      1. guess = ""
      2. lst = range(len(jumble))
      3. hint_str = '_'*len(jumble)
      4. while True:
      5. guess = raw_input("Guess or '?' or 'X': ").lower()
      6. if guess == correct:
      7. print "That's it! You guessed it!\n"
      8. break
      9. elif guess == '?':
      10. i = random.choice(lst)
      11. lst.remove(i)
      12. hint_str[i] = guess[i]
      13. print hint_str
      14. elif guess == 'x':
      15. print "Sorry you gave up!"
      16. break
      17. else:
      18. print "Sorry, thats not it. Try again."
      19. print "Thanks for playing"
      20. raw_input("\n\nPress the enter key to exit.")
      #6; Fri, 04 Jan 2008 12:52:00 GMT
    • Quote:
      === Original Words ===

      This is untested so it may not work, but maybe you will get the idea:
      Code: ( text )
      1. guess = ""
      2. lst = range(len(jumble))
      3. hint_str = '_'*len(jumble)
      4. while True:
      5. guess = raw_input("Guess or '?' or 'X': ").lower()
      6. if guess == correct:
      7. print "That's it! You guessed it!\n"
      8. break
      9. elif guess == '?':
      10. i = random.choice(lst)
      11. lst.remove(i)
      12. hint_str[i] = guess[i]
      13. print hint_str
      14. elif guess == 'x':
      15. print "Sorry you gave up!"
      16. break
      17. else:
      18. print "Sorry, thats not it. Try again."
      19. print "Thanks for playing"
      20. 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.

      #7; Fri, 04 Jan 2008 12:53:00 GMT
    • 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: ( text )
      1. # Word Jumble
      2. #
      3. # The computer picks a random word then "jumbles" it
      4. # The player has to guess the original word
      5. import random
      6. # create a sequence of words to choose from
      7. WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone",
      8. "anion", "cation", "polymorphasis", "stipulation", "antecedant")
      9. # pick one word randomly from the sequence
      10. word = random.choice(WORDS)
      11. # create a variable to use later to see if the guess is correct
      12. correct = word
      13. # create a jumbled version of the word
      14. jumble =""
      15. while word:
      16. position = random.randrange(len(word))
      17. jumble += word[position]
      18. word = word[:position] + word[(position + 1):]
      19. # sets score to zero
      20. score = 0
      21. # start the game
      22. print \
      23. """
      24. Welcome to Word Jumble!
      25. Unscramble the letters to make a word.
      26. Enter a guess, an X to give up, or type ? and press enter for a hint.
      27. (Press the enter key at the prompt to quit.)
      28. Try to get the lowest score possible. For each hint, you gain a point.
      29. See if you can win with no points!
      30. """
      31. print jumble
      32. guess = ""
      33. lst = range(len(jumble))
      34. hint_str = '_'*len(jumble)
      35. while True:
      36. guess = raw_input("Guess or '?' or 'X': ").lower()
      37. if guess == correct:
      38. print "That's it! You guessed it!\n Your score is", score
      39. break
      40. elif guess == '?':
      41. i = random.choice(lst)
      42. print correct[i], "is the", i+1, "letter."
      43. score += 1
      44. elif guess == 'x':
      45. print "Sorry you gave up!"
      46. break
      47. else:
      48. print "Sorry, thats not it. Try again."
      49. print "Thanks for playing"
      50. raw_input("\n\nPress the enter key to exit.")
      Thank you for the help!
      #8; Fri, 04 Jan 2008 12:54:00 GMT