[Tutor] Python Beginners
Steven D'Aprano
steve at pearwood.info
Wed Jun 8 12:53:25 CEST 2011
Vincent Balmori wrote:
> Hello. Right now I am learning the python language through Python Programming
> for the Absolute Beginner 3rd Edition. I am having trouble with one question in
> Ch. 4 #3, which says "Improve 'WordJumble so that each word is paired with a
> hint. The player should be able to see the hint if he or she is stuck. Add a
> scoring system that rewards players who solve a jumble without asking for the
> hint'".
>
> Right now I am having trouble with giving the 'hint' variable a value despite
> the conditions. Everything else is working fine.
Try adding an "else" clause to your "if word == ... hint = ..." block.
else:
print "word = '%s'" % word
I think you might be surprised by the value of word.
A bit more advice for you:
> # create a jumbled version of the word
> jumble =""
> while word:
> position = random.randrange(len(word))
> jumble += word[position]
> word = word[:position] + word[(position + 1):]
That's a rather complicated way to shuffle word. Here's a better way:
>>> import random
>>> word = 'python'
>>> jumble = list(word)
>>> random.shuffle(jumble)
>>> jumble = ''.join(jumble)
>>> jumble
'ytohpn'
--
Steven
More information about the Tutor
mailing list