jeudi 19 mars 2015

How to NOT repeat a random.choice when printing a piece of text in python

I have three lists in text files and I am trying to generate a 4 word message randomly, choosing from the 'prefix' and 'suprafix' list for the first 3 words and 'suffix' file for the fourth word but I want to prevent it, when printing them, from picking a word that was already chosen by the random.choice function



import random

a= random.random
prefix = open('prefix.txt','r').readlines()
suprafix = open('suprafix.txt','r').readlines()
suffix = open('suffix.txt','r').readlines()
print (random.choice(prefix + suprafix), random.choice(prefix + suprafix), random.choice(prefix + suprafix), random.choice(suffix))


As you can see it chooses randomly from those 2 lists for 3 words.


Aucun commentaire:

Enregistrer un commentaire