RSS Forums RSS
Please support our Python advertiser: Programming Forums

Starting Python

Join Date: Oct 2004
Posts: 2,548
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: Starting Python

  #18  
Jun 15th, 2005
I took this from a recent thread, to show you how you can use a temporary print statement to figure out what is going on ...
  1. # create a jumbled word/string
  2.  
  3. import random
  4.  
  5. # create a sequence, here a tuple, of words to choose from
  6. WORDS = ("python", "jumble", "easy", "difficult", "answer", "babysitter")
  7.  
  8. # pick one word randomly from the sequence
  9. word = random.choice(WORDS)
  10.  
  11. # create a variable to use later to see if the guess is correct
  12. correct = word
  13.  
  14. # create a jumbled version of the word
  15. # start with an empty string to be built up in the while loop
  16. jumble = ""
  17. # word is reduced in size by one character each time through the loop
  18. # when it is empty it will be equal to None(False) and the loop stops
  19. while word:
  20. print word,' ', jumble # for test only
  21. position = random.randrange(len(word))
  22. jumble += word[position]
  23. word = word[:position] + word[(position + 1):]
  24.  
  25.  
  26. print jumble # now you can ask to guess the word ...
Just a little note, when you save this code, don't save it as random.py. Python will confuse this in the import statement! It will look for the module random.py in the working directory first, before it goes the \Lib directory where the proper module is located.
Last edited by vegaseat : Mar 1st, 2007 at 3:41 pm. Reason: [code=python] tag
May 'the Google' be with you!
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:03 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC