Hello, I am having trouble with a python 3.2.2 problem. Here is the problem, followed by the answer I have right now (I've put the example in green for clarification):

Write a program that declares a different series of lists (lists should not have the same number of words), and asks the user how many one-line poems she would like to see. In response the program will print that many different poems.

Here is an example of how your program should behave/work:


Welcome to the Random Poems of the Day!
How many poems would you like me to generate for you? 3

Here are your 3 poems:

Paris Hilton amazingly eats mice in Duckett House every Sunday afternoon
The cat never runs on the sofa early in the morning
Mrs Christ sometimes runs on the sofa at night

Here is what I have (please excuse the length)

from random import randrange

def main():
greetings=("Welcome to Random Peems of the Day!")
poems=("How many poems would you like me to generate for you?")

#create a list of people
people=["Lil Wayne", "Nicole Richie", "The garbage man", "The Vice-President"]

#creaate a list of verbs
verb=["twirls", "shakes", "dances", "yodels", "jumps", "spits"]

#create a list of adverbs
adverb=["all the time", "once a week", "ridiculously", "strongly", "frequently"]

#create a list of locations
location=["on the hood of the car","behind the stairs", "in front of the principal"]

#create a list of times
time=["at 1 o'clock in the morning", "at sunrise", "at dinner", "at 8pm", "during brunch"]

#figure out how many people, verbs, adverbs, locations, and times are in
#people, verb, adverb, location, and time, respectively.
PeopleCount= len(people)
VerbCount=len(verb)
AdverbCount=len(adverb)
LocationCount=len(location)
TimeCount=len(time)

#pick a number at random that is between 0 (included) and
#the number of people, verbs, adverbs, locations, and times(all excluded), respetively.
randomIndex =randrange(PeopleCount, VerbCount, AdverbCount, LocationCount, TimeCount)
print(greeting)
print(poems, sep=" ")

#select the person, verb, adverb, location, and time at the Index and print words
# as a one-line poem.
print("Hey!", people[randomIndex], verb[randomIndex], sdverb[randomIndex], location[randomIndex], time[randomIndex], "!"]


main()

Every time I run my program, it says I can't assign randrange to function call, and if the user were to type in 3 for three poems, three different poems won't show up. I'm not understanding why my program isn't working. May I please have help with this? I would really appreciate it. Thank you!

What is

randomIndex =randrange(PeopleCount, VerbCount, AdverbCount, LocationCount, TimeCount)

Supposed to mean does not make sense for me, and wouldn't it be easier to use random.choice from each list?

Notice the (CODE) button on tool bar, next time push that beside pasting (I do not know how you got last ')' changed to ']') though.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I'm sorry, I thought that was the way to get a random choice from each list. This is my first time using randrange, and I'm still trying to understand it. The "}" at the end instead of a ")" was actually a typo. I'm sorry for that.

For this short code, let's consider you made good effort, here my version:

import random

def main():
    people=["Lil Wayne", "Nicole Richie", "The garbage man", "The Vice-President"]
    verb=["twirls", "shakes", "dances", "yodels", "jumps", "spits"]
    adverb=["all the time", "once a week", "ridiculously", "strongly", "frequently"]
    location=["on the hood of the car","behind the stairs", "in front of the principal"]
    time_=["at 1 o'clock in the morning", "at sunrise", "at dinner", "at 8pm", "during brunch"]

    poems = [' '.join(random.choice(a) for a in (people, verb, adverb, location, time_))
                 for count in range(int(input("How many poems would you like me to generate for you? "))) ]
    print("""
Welcome to Random Peems of the Day!

Your poems are:
""")
    print('.\n'.join(poems)+'.')


main()

If you do not understand something check it up from IDLE help menu Python documents or from python.org net site. List comprehension and multiline string could be necessary lookup words, maybe string methods -> join.

Edited 5 Years Ago by pyTony: n/a

Thank you so mucu! I checked it in python, and it worked beautifully. I will definitely look at the python manual to make sure I understand all of the syntax. Thank you once again for your help!

Hi, the program works fine, but when I type the same problem in for another problem, I keep getting this error:
Traceback (most recent call last):
File "hw3c.py", line 20, in <module>
main()
File "hw3c.py", line 11, in main
for count in range(int(input("How many poems would you like me to generate for you?")))]
File "hw3c.py", line 11, in <listcomp>
for count in range(int(input("How many poems would you like me to generate for you?")))]
AttributeError: 'list' object has no attribute 'verb'

I'm using same code that pyTony posted to center all of the poems with the longest poem starting at the leftmost column (using the max() function). When I typed it for one problem, it worked just fine. When I re-typed it the same way for another problem, I kept getting this error. May I please have help with the attribute error? Thank you so much!

Edited 5 Years Ago by computerchip: n/a

This article has been dead for over six months. Start a new discussion instead.