guys. Thanks for taking the time to look through this. I've been having some trouble trying to figure out how exactly Python is creating this new string variable. The initial code is with a tuple full of random words. There is a random module that was imported and used the choice method to pull a random word out. Then, a while loop was created from that given variable that housed the random word that was just pulled. The objective was to completely randomize the lettering of the word. After words it prints the variable that holds the randomized indexed word. Here is the code I'm talking about
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):]
print(jumble)
My question is, how exactly is python creating a brand new jumbled word? I understand the code and what is happening up until it reaches to this given part that has me confused on what's going on.
jumble += word[position]
word = word[:position] + word[(position + 1):]
Aucun commentaire:
Enregistrer un commentaire