We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,590 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Goofy Sentence Generator

Jun 15th, 2012 5:04 pm

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!

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,

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.
"""
pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 984
Skill Endorsements: 26

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!

HTMLperson5
Deleted Member

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

HTMLperson5
Deleted Member

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0615 seconds using 2.66MB