Goofy Sentence Generator

HTMLperson5 -1 Tallied Votes 556 Views Share

This is a goofy sentence generator I have made in python; it took about 5 minutes to get the code to work - but its working now
so if your curious try it out!

TrustyTony commented: Try more carefully next time! -3
from random import choice
#Values a, b, c ect.... are listed here.
a = "I"
b = "The stupid cheeser"
c = "He"
d = "She"
e = "The pirate personperson"
f = "killed"
g = "ate"
h = "vomited"
i = "ate"
j = ":)'ed"
k = "The stupid cheeser2"
l = "The moldy cheese"
m = "The cow"
valuec = [a, b, c, d, e]
value_2 = choice(valuec)
print value_2,
valueb = [f, g, h, i, j]
value_3 = choice(valueb)
print value_3,
valuea = [k, l, m]
value_4 = choice(valuea)
print value_4,
TrustyTony 888 pyMod Team Colleague Featured Poster

You have unnecessary variables, when you should use sequence and loop:

from random import choice

sentence = [
    ("I", "The stupid cheeser", "He", "She", "The pirate personperson"),
    ("killed", "ate", "vomited" "ate", ":)'ed"),
    ("the stupid cheeser2",  "the moldy cheese", "the cow")
    ]

for values in sentence:
    print choice(values),
print '.'

You are not only one who got the idea for this kind of program, actually: http://www.daniweb.com/software-development/python/threads/32007/projects-for-the-beginner#post159482
The style of code does leave also lot to be desired as it is very old program. Here what I came up by playing with vegaseats' code:

  # goofy sentence generator

import random

def make_sentence(n, *parts):
    """return n random sentences"""
    for _ in range(n):
        yield ' '.join(random.choice(p) for p in parts).capitalize() + '.'

# break a typical sentence into 3 parts
# first part of a sentence (subject)

subject_ = """\
a drunken sailor
a giggling goose
the yearning youth
the obese ostrich
this mean mouse
the skinny sister""".splitlines()

# middle part of a sentence (action)
action_ = """\
jumps over
flies over
runs across
openly ogles
twice tastes
vomits on""".splitlines()

# ending part of a sentence (object)
object_ = """\
a rusty fence
the laughing cow
the weedcovered backyard
the timid trucker
the rancid old cheese
the jolly jelly""".splitlines()

print '-'*60

for item in make_sentence(3, subject_, action_, object_):
    print item
print '-'*60

"""
a typical result -->
A drunken sailor flies over the laughing cow.
The obese ostrich runs across the weedcovered backyard.
This mean mouse openly ogles the jolly jelly.
"""
Member Avatar for HTMLperson5
HTMLperson5

Hmmm....
well, vegaseat has done about the same thing as I had done previously 43 lines of code when tweaked and 59 left as original! My original program took up 24 lines of code and the modified version took up 11! Yet my program has the same output (or at least performs the same function) in far less code!

Member Avatar for HTMLperson5
HTMLperson5

How come vegaseats' code is so much longer? What more does it do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.