473,387 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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



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



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:
Expand|Select|Wrap|Line Numbers
  1. The jumble is: tufdilcfi
  2. Hint string: _________
  3. Hint string: ___f_____
  4. Hint string: ___f____t
HTH :)
Mar 8 '07 #4
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!
Mar 14 '07 #5
dshimer
136 Expert 100+
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.
Expand|Select|Wrap|Line Numbers
  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()
  10.  
Mar 14 '07 #6
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  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.  
  20. print "Thanks for playing"
  21.  
  22. raw_input("\n\nPress the enter key to exit.")
Mar 14 '07 #7
This is untested so it may not work, but maybe you will get the idea:
Expand|Select|Wrap|Line Numbers
  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.  
  20. print "Thanks for playing"
  21.  
  22. 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.
Mar 15 '07 #8
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:

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

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
56
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
9
by: ToyImp | last post by:
I am wanting to start getting into games programming with visual basic and DX. What I need to know is what is the best version of VB to use. I currently have 6.0 that I got from my college that I...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
4
by: jackjack | last post by:
Hiya, I'm completely useless with Visual Basic but this should be pretty easy to do. This is regarding MACROS in Microsoft Word by the way... Basically I want to open a word document and I...
2
by: terry guthrie | last post by:
I need some help with visual basic code, I need to create a timer that will be visible while playing a game, i know there is a timer in the tools that you pull over to the project and is hidden, and...
6
by: Ken Foskey | last post by:
I am brand new to coding C# and Visual Studio. I have worked out how to configure the ComboBox creating my own object however it leave me with a lot of management updating as the table changes....
10
by: longshan | last post by:
Hi friends, Can you help me write some code to list all the arrangement of a word? for example "unix" it has possibilities like nixu,ixun,,,and so on. I think we can get 4!(4*3*2*!)=24...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.