View Single Post
Join Date: Aug 2005
Posts: 1,514
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Improve scrambled text game

 
0
  #6
Sep 14th, 2008
You can also let Python do the jumbling of the words and create the dictionary of word:jumble pairs:
  1. # create a dictionary of random jumbled words for a game
  2.  
  3. import random
  4.  
  5. def jumble_word(word):
  6. # create a list of characters of the word
  7. char_list = list(word)
  8. # shuffle sequence in place
  9. random.shuffle(char_list)
  10. # joint to form a word again
  11. return "".join(char_list)
  12.  
  13. # add more words to the list as needed
  14. words = ['armada', 'bubble', 'letter', 'banana', 'radio', 'hammer',
  15. 'spark', 'pearl', 'target', 'zappy', 'zipper', 'organist',
  16. 'kitchen', 'ruler', 'motorist', 'polar', 'garage', 'window']
  17.  
  18. # create a list of jumbled words
  19. jumbles = []
  20. for word in words:
  21. jumbles.append(jumble_word(word))
  22.  
  23. # create a dictionary from the two list
  24. words_dict = dict(zip(words, jumbles))
  25.  
  26. print words_dict
  27.  
  28. """
  29. possible output -->
  30. {'polar': 'aropl', 'target': 'rgaett', 'ruler': 'rleur',
  31. 'garage': 'eaaggr', 'bubble': 'beblub', 'window': 'nowdiw',
  32. 'pearl': 'aprel', 'zipper': 'pperzi', 'radio': 'idoar',
  33. 'motorist': 'rtotimos', 'letter': 'rettel', 'zappy': 'pzpay',
  34. 'armada': 'ardaam', 'spark': 'akprs', 'kitchen': 'theinck',
  35. 'hammer': 'mrameh', 'banana': 'bnanaa', 'organist': 'soiatgnr'}
  36. """
drink her pretty
Reply With Quote