[Tutor] Word Jumble - Chpt4 (Explanation)
Prasad, Ramit
ramit.prasad at jpmorgan.com
Fri May 17 22:48:39 CEST 2013
Please always send to the list (use your email client's Reply-to-list or Reply-all functionality).
Arvind Virk wrote:
> Thanks Ramit!
>
> I did try to use print statements to understand my outputs but I may have been best placed to utilse
> some formatting to make it more readable. I did not understand why the loop did not terminate after it
> had read the word once but from your example,
> I can see it is continually looping until it has utilised all the characters in the word?
>
While loops continue until their condition evaluates to false.
Strings evaluate to false when they are empty strings (e.g. '').
Containers evaluate to false when they have no contents.
This applies to if statements too.
if cookiejar:
# pass jar and eat cookie
I am fairly confident you do not want the cookie jar if
it is empty or at least your actions will be different. :)
> line 1 is the word
> line 2 is a empty string
> line 4 is creating a range based on the length of word
>
> line 6 is the letter of the word
> line 7 new letter for jumble
> line 8 word from beginning upto position + position onward? or one after position?
>
> line 5 and 9 - I know these are your print statements but I haven't been introduced to .format (yet).
> How does printing jumble {0} return the entire jumble string? Why does word have to be {1}?
>
String formatting has a mini-language of options.
See http://docs.python.org/2/library/string.html#formatstrings
Basically {0} means the first argument passed to format while {1}
means the second where the Nth argument will be {N-1}. I could also
have used named formatting by using {placeholder} and
.format( placeholder='blah' ).
>>> 'After jumble "{jumble}" | word "{wrd}" | tempchar {char}'.format( jumble='jumble', wrd='some word here', char='more than a char' )
'After jumble "jumble" | word "some word here" | tempchar more than a char'
>
> 1. word = 'python'
> 2. jumble = ""
> 3. while word:
> 4. position = random.randrange(len(word))
> 5. print 'position {0} | jumble "{1}"'.format( position, jumble )
> 6. tempchar = word[position]
> 7. jumble += tempchar
> 8. word = word[:position] + word[(position+1):]
> 9. print 'After jumble "{0}" | word "{1}" | tempchar {2}'.format( jumble, word, tempchar )
>
> > position 5 | jumble ""
> > After jumble "n" | word "pytho" | tempchar n
> > position 2 | jumble "n"
> > After jumble "nt" | word "pyho" | tempchar t
> > position 3 | jumble "nt"
> > After jumble "nto" | word "pyh" | tempchar o
> > position 1 | jumble "nto"
> > After jumble "ntoy" | word "ph" | tempchar y
> > position 0 | jumble "ntoy"
> > After jumble "ntoyp" | word "h" | tempchar p
> > position 0 | jumble "ntoyp"
> > After jumble "ntoyph" | word "" | tempchar h
>
> Thanks very much. It really helped.
> -
> Arvind Virk
~Ramit
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Tutor
mailing list