[Courses] [python] Lesson 5: Infinite loops, modulo, and random numbers

lenore borisova orangegirl at gmail.com
Sun Jul 17 20:43:44 UTC 2011


Homework: Lesson 5: Infinite loops, modulo, and random numbers


1. Colors & things:

color = [ "red", "orange", "yellow", "green", "blue" ]

things = 20

for i in range (0,things) :
        print color[i % len(color)]



2. Random dir

import os, random

twins = "/users/lenore/desktop/twins/"

print random.choice(os.listdir(twins))



3. Ad-lib

import random

verb = ["sneezed", "hugged", "yawned", "cried", "waited", "hovered"]

noun = ["balloon", "submarine", "mushroom", "baby", "tree", "meteor",
"drunk"]

print "The", random.choice(noun), random.choice(verb)+"."



4. Input + mix up sentence type + add adjective



import random

iverb = ["sneezed", "hugged", "yawned", "cried", "waited", "hovered"]

tverb = ["punished", "tested", "kicked", "painted", "eats"]

noun = ["balloon", "submarine", "mushroom", "baby", "tree", "meteor",
"drunk"]

adj = ["obnoxious", "orange", "fancy", "old", "bitter", "chubby"]


while True:

        adlib = raw_input("Enter a noun or q to quit: ")

        seed = random.randint(1,6)

        if len(adlib) % seed >= 3 :
                print "The", random.choice(adj), adlib,
random.choice(iverb)+"."
        else :
                print "The", adlib, random.choice(tverb), "the",
random.choice(noun)+"!"

        if adlib == "q" :
                print "adios! bye-bye! ciao! salaam! poka!"
                break


More information about the Courses mailing list