python - Random Phrase Creator -


i want create program generate random phrase creating 10 random integers 1 20, , depending on each variables integer, word or phrase produced. there easier way following:

#this 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 on... p.s. using python 3

thank helpful advice. new python, , helpful have people can me create better code. if cares, used program talks you, , offers chance hear jokes , play several games. have nice day.

pps ended going with:

import random words = 'squirrel orca ceiling crayon boot grocery jump' .split() def getrandomword(wordlist):     # function returns random string passed list of strings.     wordindex = random.randint(0, len(wordlist) - 1)     return wordlist[wordindex] potato = getrandomword(words) print (potato) # plus of course other words... base. 

sure. use python lists and/or dictionaries. if choose 1 group:

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

if not, can use nested list:

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

this can extended number of groups , words in each group.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -