| | |
while loop question
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
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 :-)
[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 :-)
Many times you can insert a temporary print statement to show what is going on. Here a test print inside the loop ...
The empty string would return a None (Python's version of NULL) which is evaluated as boolean False, so the loop stops.
Python Syntax (Toggle Plain Text)
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: print word,' ', jumble # for test only position = random.randrange(len(word)) jumble += word[position] word = word[:position] + word[(position + 1):] 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!
![]() |
Similar Threads
- C++ For Loop User Response (C++)
- Simple script loop question (Shell Scripting)
- C++ Loop Question Here (C++)
- C++ For Loop Question (C++)
- how to use loop in this question (C++)
- need help w/for loop (C++)
- I need to re-write this code using a 'while' loop. (Java)
Other Threads in the Python Forum
- Previous Thread: break statement
- Next Thread: tuple or list?
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang slicenotation split string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web windows wx.wizard wxpython xlwt






