944,155 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5662
  • Python RSS
Jun 14th, 2005
0

while loop question

Expand Post »
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 :-)
Similar Threads
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jun 14th, 2005
0

Re: while loop question

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.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Jun 15th, 2005
0

Re: while loop question

Many times you can insert a temporary print statement to show what is going on. Here a test print inside the loop ...
Python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 15th, 2005
0

Re: while loop question

thanks for the suggestion. That technique should come in handy.
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: break statement
Next Thread in Python Forum Timeline: tuple or list?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC