while loop question

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

while loop question

 
0
  #1
Jun 14th, 2005
I am trying to follow this code I am reading in a book. First I will show you the code as the book has it. This is for a word jumble program. This algorythm I am refering to jumbles a word
[php]
import random



# create a sequence of words to choose from

WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")

# 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):][/php]

here is where I am a little unsure. is the statement
[php]
while word:[/php]
the same as
[php]while word != "":[/php]

assuming the above two statments are the same. I am haveing trouble seeing why the variable word will ever evaluate to an emptly set. It seems like the while loop would concatenate forever, building a large and larger word. Any help in understanding this would be appreciated :-)
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: while loop question

 
0
  #2
Jun 14th, 2005
I am embarresed to say how long this took me to figure out. everytime it loops, one letter is removed from the variable word. Eventually it becomes an emptly set, then the loop stops.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: while loop question

 
0
  #3
Jun 15th, 2005
Many times you can insert a temporary print statement to show what is going on. Here a test print inside the loop ...
  1. import random
  2.  
  3. # create a sequence of words to choose from
  4. WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
  5.  
  6. # pick one word randomly from the sequence
  7. word = random.choice(WORDS)
  8.  
  9. # create a variable to use later to see if the guess is correct
  10. correct = word
  11.  
  12. # create a jumbled version of the word
  13. jumble =""
  14.  
  15. while word:
  16. print word,' ', jumble # for test only
  17. position = random.randrange(len(word))
  18. jumble += word[position]
  19. word = word[:position] + word[(position + 1):]
  20.  
  21.  
  22. print jumble

The empty string would return a None (Python's version of NULL) which is evaluated as boolean False, so the loop stops.
Last edited by vegaseat; Aug 12th, 2009 at 12:59 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: while loop question

 
0
  #4
Jun 15th, 2005
thanks for the suggestion. That technique should come in handy.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC