#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Need Help with Jumble Python Program


    I need to know if someone could please explain how this program works.
    The part that I am not understanding is the part where the jumbled word is created.
    I need to understand how the "while loop" works in this program:

    # Word Jumble Game

    #

    # The computer picks a random word and then "jumbles" it.

    # The player has to guess the original word.

    #

    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[osition] + word[(position + 1):]
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Let's insert some print statements and run it a couple times:
    Code:
    import random
    WORDS = 'python jumble easy difficult answer xylophone'.split()
    correct = word = random.choice(WORDS)
    jumble = ''
    print('the word is',correct)
    while word:
        a = 'len(%s) == %d'%(word,len(word))
        position = random.randrange(len(word))
        b = 'from position %d remove character %c'%(position,word[position])
        jumble += word[position]
        word = word[:position] + word[(position + 1):]
        print(a,b,'appended to jumble',jumble)
    print(jumble)
    Code:
    $ python /tmp/p.py
    ('the word is', 'jumble')
    ('len(jumble) == 6', 'from position 3 remove character b', 'appended to jumble', 'b')
    ('len(jumle) == 5', 'from position 2 remove character m', 'appended to jumble', 'bm')
    ('len(jule) == 4', 'from position 0 remove character j', 'appended to jumble', 'bmj')
    ('len(ule) == 3', 'from position 0 remove character u', 'appended to jumble', 'bmju')
    ('len(le) == 2', 'from position 0 remove character l', 'appended to jumble', 'bmjul')
    ('len(e) == 1', 'from position 0 remove character e', 'appended to jumble', 'bmjule')
    bmjule
    $ python /tmp/p.py
    ('the word is', 'xylophone')
    ('len(xylophone) == 9', 'from position 4 remove character p', 'appended to jumble', 'p')
    ('len(xylohone) == 8', 'from position 5 remove character o', 'appended to jumble', 'po')
    ('len(xylohne) == 7', 'from position 6 remove character e', 'appended to jumble', 'poe')
    ('len(xylohn) == 6', 'from position 3 remove character o', 'appended to jumble', 'poeo')
    ('len(xylhn) == 5', 'from position 0 remove character x', 'appended to jumble', 'poeox')
    ('len(ylhn) == 4', 'from position 3 remove character n', 'appended to jumble', 'poeoxn')
    ('len(ylh) == 3', 'from position 0 remove character y', 'appended to jumble', 'poeoxny')
    ('len(lh) == 2', 'from position 0 remove character l', 'appended to jumble', 'poeoxnyl')
    ('len(h) == 1', 'from position 0 remove character h', 'appended to jumble', 'poeoxnylh')
    poeoxnylh
    $
    and finally, let's observe that the author didn't know about random.shuffle
    Code:
    >>> a = list('jumble')
    >>> random.shuffle(a)
    >>> a
    ['e', 'l', 'm', 'b', 'u', 'j']
    >>> ''.join(a)
    'elmbuj'
    >>>
    Last edited by b49P23TIvg; October 2nd, 2012 at 01:51 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    reply


    Thanks b49P23TIvg. But I'm new to Python so I need some more detail on what you are trying to explain. The two statements I have the most trouble understanding is "position = random.randrange(len(Word))" and "word=word[osition]+word[(position+1):]"....Thanks.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,856
    Rep Power
    481
    Please disable smilies in your posts, and please use code tags. Follow link in my signature for advice about that.

    Next, it's the slicing that's confusing. First, try this experiment designed to teach you to become self sufficient in python. In the python interpreter:

    >>> import random
    >>> help(random.randrange)


    Now, about slicing:
    >>> help(slice)
    might be somewhat useful, better:
    click this link then search for `slice'

    ignore the rest of this message.

    In your code, randrange(n) returns an integer between 0 and n-1.

    Great! random.randrange(len(Word)) returns the index of some character within Word.

    Anyway, that part didn't confuse you. It's badly written code. Word is reused but you have plenty of memory for a new variable.

    word=word[osition]+word[(position+1):]
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo