[Tutor] space between words printed

Alex Kleider akleider at sonic.net
Sun Nov 3 17:14:40 CET 2013


On 2013-11-02 20:18, Byron Ruffin wrote:
> The output generates a sentence made up of words chosen randomly from
> lists.  I am having trouble getting a space between each of the
> words.  Should I be thinking about .split?  Here is the code (ignore
> indent errors as it was copied and pasted) Thank you:
> 
> import random
> 
> def wordList():
> 
>     adj1 = ["Big",        "Small",  "Early", 
> "Late",    "Red",       "Tall",    "Short"]
>     subj = ["politician", "man",    "woman",  "whale",  
> "company",   "child",   "soldier"]
>      obj =  ["budget",     "money",  "box",   
> "gift",    "gun",       "tank",    "drone"]
>     adj2 = ["hot",        "crazy",  "stupid", "fast",   
> "worthless", "awesome", "dirty"]
>      verb = ["spends",     "shoots", "evades", "pursues",
> "subverts",  "passes",  "flirts"]
> 
>     y = adj1[generate()], subj[generate()] + obj[generate()] +
> adj2[generate()] + verb[generate()]
> 
>     return y
> 
> def generate():
>     random0_6 = random.randint(0, 6)
>     return random0_6
> 
> def main():
>    
>     print (wordList(), ".", sep="")
> 
> main()

I would suggest that 'wordList' return a tuple (which it sort of does 
but not quite what I have in mind:-) making eventual printing much 
easier as in """ print "%s %s %s %s %s." % wordList """.  (Modification 
needed to make it Python3 vs 2.7, I believe.)



More information about the Tutor mailing list