2

I want to create a program that will generate a random phrase by creating 10 random integers that are from 1 to 20, and depending on each variables integer, a certain word or phrase will be produced. Is there an easier way to do it than the following:

#This is a random phrase generator
import random
Rand1 = random.randint (1, 20)
Rand2 = random.randint (1, 20)
Rand3 = random.randint (1, 20)
Rand4 = random.randint (1, 20)
Rand5 = random.randint (1, 20)
Rand6 = random.randint (1, 20)
Rand7 = random.randint (1, 20)
Rand8 = random.randint (1, 20)
Rand9 = random.randint (1, 20)
Rand10 = random.randint (1, 20)
    if Rand1 ==1:
        print ('Squirrel')

and so on... P.S. Using Python 3

Thank you for the helpful advice. I am new to python, and it is very helpful to have people who can help me create better code. If anyone cares, I used this for a program that talks to you, and offers you the chance to hear jokes and play several games. Have a nice day.

PPS I ended up going with:

import random
words = 'squirrel orca ceiling crayon boot grocery jump' .split()
def getRandomWord(wordList):
    # This function returns a random string from the passed list of strings.
    wordIndex = random.randint(0, len(wordList) - 1)
    return wordList[wordIndex]
potato = getRandomWord(words)
print (potato) # plus of course all the other words... this is just the base.
John Campbell
  • 65
  • 1
  • 7

4 Answers4

10

Sure. Use Python lists and/or dictionaries. If you choose from one group:

words = ['Some', 'words', 'any', 'number', 'of', 'them']
choices = [random.choice(words) for _ in range(10)]
print(' '.join(choices))

If not, you can use a nested list:

words = [['Sam', 'Joe'], ['likes', 'hates'], ['apples', 'drugs', 'jogging']]
choices = [random.choice(group) for group in words]
print(' '.join(choices))

This can be extended to any number of groups and words in each group.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • 1
    Second one here is what I was going to suggest. – Glitch Desire Apr 19 '13 at 21:44
  • Hours of mirth ensue when expanding the list of words. For example: The frustrated bicycle produces democracy in the manner of James Franco **AND** The wet beaver exterminates children for Walter White. – Agi Hammerthief Jul 12 '16 at 14:09
4

There are a variety of easier ways. The first that comes to mind is to have a list of the phrases and then use random.choice instead (that way you also don't have to worry about adding new words and adjusting.

Example:

import random

part1 = ("Cat","Dog","Squirrel")
part2 = ("jumps", "walks", "crawls")
part3 = ("under","over")

print (random.choice(part1) + " " +  random.choice(part2))

(Edited to use the ('s around print for Python 3)

You could also use a list of lists to build the whole string...

words = (part1, part2, part3)
print (" ".join([random.choice(word) for word in words]))
Foon
  • 6,148
  • 11
  • 40
  • 42
0
import random
Rand1 = random.randint (1, 4)
random1words = ['','Squirrel','Falcon','Pig']

print (random1words[Rand1])

would almost certainly be better

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

Here, this is originally taken from my book code(a form of a cipher) implementation (Book cipher) taken from here: arnold/book cipher with python

But I changed a few pieces around in my code to make it fit your description.

BOOK="shakespeare.txt" # Your book/document .txt file containing words (maybe, articles from wikipedia, whatever,)
#the content from this file (in this case, shakespeare.txt); will be taken and used in the random-word/phrase progress.


def GetBookContent(BOOK):
    ReadBook = open(BOOK, "r")# Open <BOOK>(shakespeare.txt in this case) in reading mode, into ReadBook
    txtContent_splitted = ReadBook.read();# assign the contents(splitted!) from <BOOK> into -> txtContent_splitted
    ReadBook.close()#Closing the book
    Words=txtContent_splitted

    return( txtContent_splitted.split() )

boktxt = GetBookContent(BOOK)
K
#Randomness happens here. We import random, assigns a range 
#(in a form of a list!) 
#from 0 to 10 into the variable K.
#and we shuffle K, (hence, we get the random word-indexes)

import random; K=list(range(0,10)); random.shuffle(K);

x=0;klist=K
for keys in klist: print(boktxt[int(klist[x])]);x=x+1
    
#Test Run:
#generating 10 random words.
#          Before
#          speak.
#          me
#          hear
#          any
#          what 
#          proceed
#          further,
#          First
#          we
William Martens
  • 753
  • 1
  • 8
  • 27