Midterm Exam 2 Review Spring 2004: Gendered random sentences
Comments, answers, and questions go here:
(Link back to Sp2004 Midterm 2 Review)
import random
def sentence(g):
fnouns =["Angela"]
mnouns = ["Mark", "Adam", "Larry", "Jose", "Matt", "Jim"]
verbs = ["runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"]
phrases = ["in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique"]
phrases = phrases + ["very badly", "while skipping","instead of grading", "while typing on the CoWeb."]
if ????????
print random.choice(nouns), random.choice(verbs), random.choice(phrases)
So I made a "mnouns" for male nouns, and "fnouns" for female nouns, but when i went to put in my IF statement, i got confused. How do I pull out information from teh input section? like, "if the input =f, or IF the input=M"? I dont know how to reference the INPUT in an IF statement. Does taht make sense?
Amelia Cipolla
import random
def sentence(gender):
fnouns =["Angela"]
mnouns = ["Mark", "Adam", "Larry", "Jose", "Matt", "Jim"]
verbs ="runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"]
phrases = ["in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique"]
phrases = phrases + ["very badly", "while skipping","instead of grading", "while typing on the CoWeb."]
if gender=m
print random.choice(mnouns), random.choice(verbs), random.choice(phrases)
if gender=f
print random.choice(fnouns), random.choice (verbs), random.choice (phrases)
is this it? i think i may have just been panicing.
Amelia Cipolla
| Close. m and f aren't valid variables though. I think you might want them as strings? And you test equality with "==". Mark Guzdial |
import random
def sentence(gender):
fnouns =["Angela"]
mnouns = ["Mark", "Adam", "Larry", "Jose", "Matt", "Jim"]
verbs ="runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"]
phrases = ["in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique"]
phrases = phrases + ["very badly", "while skipping","instead of grading", "while typing on the CoWeb."]
if gender=="m"
print random.choice(mnouns), random.choice(verbs), random.choice(phrases)
if gender=="f"
print random.choice(fnouns), random.choice (verbs), random.choice (phrases)
Why is JES telling me I have a spacing error in line 10, the print.random line? Is everything else correct?
Catherine Covington
You are missing a "(" before "runs"
I added the colons, but it's still telling me I have a spacing error. Please help:
if gender=="m":
print random.choice(mnouns), random.choice(verbs), random.choice(phrases)
if gender=="f":
print random.choice(fnouns), random.choice (verbs), random.choice (phrases)
Catherine Covington
For this question are we supposed to generate just one male and one female name for the sentences or can we do it like the above and use multiple names for each? Lindsey Richardson
import random
def sentence(g):
fnouns =["Angela"]
mnouns = ["Mark", "Adam", "Larry", "Jose", "Matt", "Jim"]
verbs = ["runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"]
phrases = ["in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique"]
phrases = phrases + ["very badly", "while skipping","instead of grading", "while typing on the CoWeb."]
if g== "M":
print random.choice(mnouns), random.choice(verbs), random.choice(phrases)
if g== "F":
print random.choice(fnouns), random.choice(verbs), random.choice(phrases)
Margaret McIntosh
import random
def randomSentence(gender):
fnouns =("Angela")
mnouns = ("Mark", "Adam", "Larry", "Jose", "Matt", "Jim")
verbs = ("runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles")
phrases = ("in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique")
phrases = phrases + ("very badly", "while skipping","instead of grading", "while typing on the CoWeb.")
if gender == "m":
print random.choice(mnouns),random.choice(verbs),random.choice(phrases)
if gender == "f":
print random.choice(fnouns),random.choice (verbs),random.choice (phrases)
Susan Kommeth
I don't understand how to test functions that start with "import"-whatever...
Catherine, you have an extra space in your random.choice(verbs) for if gender=="f". Hope that helps.
Stephanie Weitzel
The questions is asking you to make modifications to the given program. After making these changes, you can save and load your program and then test it. To do that, you would write the program name in the command area along with the input and hit enter. That should generate the random sentences. Stephanie Weitzel
| import makes available additional functions from a module. random is a module that provides the choice function that chooses a random item from a list. Mark Guzdial |
i'm still not understanding what you have to put in the command area for this to work...any help would be greatly appreciated!
import random
def sentence(gender):
malenouns = ["Mark", "Adam", "Larry", "Jose", "Matt", "Jim"]
femalenouns = ["Angela", "Amy", "Sarah", "Maggie"]
verbs = ["runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"]
phrases = ["in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique"]
phrases = phrases + ["very badly", "while skipping","instead of grading", "while typing on the CoWeb."]
if gender == "M":
print random.choice(malenouns), random.choice(verbs), random.choice(phrases)
if gender == "F":
print random.choice(femalenouns), random.choice(verbs), random.choice(phrases)
This is what I get from the code above, using both M and F:
From the command area of JES:
>>> sentence("M")
Larry climbs while reading the Technique
>>> sentence("M")
Larry runs while skipping
>>> sentence("M")
Jim skips in a tree
>>> sentence("M")
Matt skips over a log
>>> sentence("M")
Adam argues in a tree
>>> sentence("F")
Maggie skips while skipping
>>> sentence("F")
Angela argues instead of grading
>>> sentence("F")
Maggie climbs in a tree
>>> sentence("F")
Sarah climbs while skipping
>>>
So... the code above works!poof #2
Are we supposed to have just one phrase at the end of the sentence? Kristin Noell
Link to this Page