toast? ok so im working on a program that takes a randomly generated word n scrambles all the letters out
then it spits out the letters (which are still scrambled) it will ask the users to type what they think the word is they get 10 tries before they lose
the code so far has no bugs in it i just need to know the small piece / block of code that will scramble the letters in randwrd (this will be seen in the code)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | # Text Twister
# Nik Uzelac
# Octoer 29, 2009
# This program is self explanitory, if you dont kno what it does by the name.... the ur just retarted.
import time
import random
import os
os.system ('color a')
################## File2List Begin: This code is to read a file into a list ##
## Copy and paste the below code into your program if you wish ##
filein = open("TEXTTWIST_words.txt", "r")
line_list = filein.readlines()
c = 0
while c < len(line_list): # This will go through each item of the list and resave it without the new line character
line_list[c]=line_list[c].replace('\n', '') # replaces the newline character by a blank
c = c + 1
## Copy all the way until here ##
################## File2List End ##########################################
print "Type 'start' to play the game and 'exit' to leave the game"
game = raw_input()
os.system('CLS')
while game == "start" :
print "Instructions will be shown momentarily"
time.sleep(3)
os.system('CLS')
print " INSTRUCTIONS"
time.sleep(3)
print " you will recive a bunch of scrambled letters"
time.sleep(4)
print " rearrange the letters till you get the word that the computer is thinking of"
time.sleep(6)
print " you will get 10 trys before you get the game over"
time.sleep(4)
print " NOW PLAY"
time.sleep(2)
os.system('CLS')
randwrd = line_list[random.randint(0,len(line_list)-1)]
time.sleep(23)
if game != "start" :
print "Game closing"
time.sleep(3)
|
Look into the itertools module (specifically the permutations function).
e.g.: list(itertools.permutations('FOOD', 4))
evaluates to:
[('F', 'O', 'O', 'D'), ('F', 'O', 'D', 'O'), ('F', 'O', 'O', 'D'), ('F', 'O', 'D', 'O'), ('F', 'D', 'O', 'O'), ('F', 'D', 'O', 'O'), ('O', 'F', 'O', 'D'), ('O', 'F', 'D', 'O'), ('O', 'O', 'F', 'D'), ('O', 'O', 'D', 'F'), ('O', 'D', 'F', 'O'), ('O', 'D', 'O', 'F'), ('O', 'F', 'O', 'D'), ('O', 'F', 'D', 'O'), ('O', 'O', 'F', 'D'), ('O', 'O', 'D', 'F'), ('O', 'D', 'F', 'O'), ('O', 'D', 'O', 'F'), ('D', 'F', 'O', 'O'), ('D', 'F', 'O', 'O'), ('D', 'O', 'F', 'O'), ('D', 'O', 'O', 'F'), ('D', 'O', 'F', 'O'), ('D', 'O', 'O', 'F')]
So you'll need to ''.join(each_entry).
Go with random.shuffle.