Python: How to print my simple poem -
i know how print out sentences using triplet poem program. program randomly picks list of nouns use.
my program:
import random def nouns(): nounpersons = ["cow","crowd","hound","clown"]; nounplace = ["town","pound","battleground","playground"]; rhymes = ["crowned","round","gowned","found","drowned"]; nounpersons2 = ["dog","frog","hog"]; nounplace2 = ["fog","prague","smog"]; rhymes2 = ["log","eggnog","hotdog"]; nounlist1 = [nounpersons,nounplace,rhymes] nounlist2 = [nounpersons2,nounplace2,rhymes2] nounslist = [nounlist1, nounlist2] randompick = random.choice(nounslist) return(randompick) verbs = ["walked","ran","rolled","biked","crawled"]; nouns() for example, have "the cow walked town. drowned." , replace nouns/rhyme(cow, town,drowned) , verb(walked) randomizer.
would use random.randint in way?
i need general print statement example showed using randomizer randomly pick between nouns/rhymes.
as usual (for me), there may more pythonic approach, have working, did 3 things:
assigned call nouns() function 'chosen_list' variable. way returned 'randompick' gets used.
built in selection step individual words lists in 'chosen_list' , verb list
added final print statement formatting assemble words in sentence
the code:
import random def nouns(): nounpersons = ["cow","crowd","hound","clown"]; nounplace = ["town","pound","battleground","playground"]; rhymes = ["crowned","round","gowned","found","drowned"]; nounpersons2 = ["dog","frog","hog"]; nounplace2 = ["fog","prague","smog"]; rhymes2 = ["log","eggnog","hotdog"]; nounlist1 = [nounpersons,nounplace,rhymes] nounlist2 = [nounpersons2,nounplace2,rhymes2] nounslist = [nounlist1, nounlist2] randompick = random.choice(nounslist) return randompick verbs = ["walked","ran","rolled","biked","crawled"] # change 1. chosen_list = nouns() # select single words lists - change 2. noun_subj = random.choice(chosen_list[0]) noun_obj = random.choice(chosen_list[1]) rhyme_word = random.choice(chosen_list[2]) verb_word = random.choice(verbs) # insert words in text line - change 3. print ("the {} {} {}. {}.".format(noun_subj, verb_word, noun_obj, rhyme_word))
Comments
Post a Comment