In the program be below, I'M having trouble with these few lines, remember, I'M new.

What's going on here? I'M just a little confused because there is (word) wraped in len().

position = random.randrange(len(word))

And what's going on with these two lines? Thanks.

jumble += word[position]
    word = word[:position] + word[(position + 1):]
# Word Jumble
#
# 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[:position] + word[(position + 1):]
    
# start the game
print \
"""

            Welcome to Word Jumble!
            
        Unscramble the letters to make a word.
    (Press the enter key at the promt to quit.)
"""
print "The jumble is:", jumble

guess = raw_input("\nYour guess: ")
guess = guess.lower()

while(guess != correct) and (guess != ""):
    print "Sorry, that's not it."
    guess = raw_input("Your guess: ")
    guess = guess.lower()
    
if guess == correct:
    print "That's it! You guessed it!\n"
    
print "Thanks for playing."

raw_input("\n\nPress the enter key to exit.")

Recommended Answers

All 2 Replies

If you want to know what is going on, put in a temporary test print ...

...
...
while word:
    position = random.randrange(len(word))
    
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
    # for test only ...
    print position, jumble, word
...
...

Print is good to use.
IDLE is good to use,to se what going on.

Just a little test

>>> import random
>>> WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
>>> word = random.choice(WORDS)
>>> word
'python'
>>> # So now we have a random word from tuple list

# This should give a random number from the word 'python'
>>> position = random.randrange(len(word))
>>> position
5
>>> # 5 is is "n" remember we count from 0
>>> word[position] # So this should now give "n"
'n'
>>>

Like this you can break upp the script to understand what happends.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.