# Python Word Jumble Game

Can the following complete program be improved? I have been using the Python For Absolute Beginners book and recently made my own version of the Python Word Jumble game.

The Areas I am particularly interested in improving:

1. The while word Loop Block
2. Python Standards
3. Simplifying the overall program
4. Commenting
# Word Jumble Game. Using Mac0S (Latest). Python V 3.4.3

import random

# Initialize Variables
WORD = ('python', 'programming', 'mug', 'glass','phone') # Constant Variable
word = random.choice(WORD)
correct = word
jumble = ''
guess = ''

print('\t\tWelcome to Word Jumble!')
print('\n')
print('\tUnscramble the letters to make a word.')

while word:
position = random.randrange(len(word))
jumble += word[position] # Jumble process 1
word = word[:position] + word[(position + 1):] #Jumble process 2

print('\n')
print('The Jumble is:', jumble)

while guess != correct:
print('Sorry, that\'s not it')

if guess == correct:
print('\n')
print('That\'s it! You guessed it!')
print('\n')
print('Thanks for playing')

exit() # User has the correct answer


In general this codes looks pretty good. A couple of nits.

### The Python Library is Huge:

I would suggest replacing the while word: loop block:

while word:
position = random.randrange(len(word))
jumble += word[position] # Jumble process 1
word = word[:position] + word[(position + 1):] #Jumble process 2


With a library call like:

jumble = ''.join(random.sample(word, k=len(word)))


### Main loop:

By using a break inside the main loop, it can be restructured to remove some extra code like:

while True:
print('\nThe Jumble is:', jumble)
if guess == word:
print("\nThat's it! You guessed it!\n")
print('Thanks for playing')
break

print("Sorry, that's not it")


This removes an extra comparison and input()

I am not a fan of right hand style comments. I generally prefer a needed comment to proceed the line/block it is commenting. You used reasonably descriptive variable names, I don't know that anything other than the header comment is providing much value.

The main improvement for your code is provided by Stephen's answer. All what I could highlight would be minor improvements except this one: Your code is too sequential, non object-oriented, not even functional. This is a big penalty impeding you or anyone else to reuse or scale it. This means you simply need to re-design your program, and preferably using OOP concepts.

Comments must be useful. Comments like # Initialize Variables are not very good because even the vert beginner programmer can understand you are initializing those variables. This goes mostly right with the remaining comments except the very first one which describes what is going on with your module.

• Great. Thank you Billal. I will be using OOP soon, however I am yet to learn it. What kind of things would you deem necessary to comment on in fairly complex programs (Loops, Def Functions)? Greg. – Greg Jun 9 '17 at 14:33