[Courses] [python] Lesson 5: Infinite loops, modulo, and random numbers
Prana Peddi
prana.lists at gmail.com
Mon Jul 18 18:28:19 UTC 2011
My answers below: I did not use modulo in question 4 below.
1. Suppose you have a list of colors, like
> colors = [ "red", "orange", "yellow", "green", "blue" ]
> and you have a bunch of things (say, 20 of them) that you
> need to color without repeating. How would you use the modulo
> operator, %, to step through the colors in order, printing out one
> after another and going back to red after blue?
>
> Hint: remember, x % y always gives you a number between 0 and y.
> What does y have to be so that it always gives you something that
> could be an index of the list?
>
colors = ["red", "orange", "yellow", "green", "blue"]
for i in range(0,20):
j = i % len(colors)
print "item {0} needs to have color {1}.".format(i, colors[j])
2. How would you write a program to choose a random file from a
> directory full of files? Assume the directory is in some fixed place,
> like "/usr/share/backgrounds" or "/home/yourname/Backgrounds".
> Hint: os.listdir(dirname) will give you a list of all the files in
> a directory. Of course you have to import os first.
>
> Why would you want to do this? In my case, I have a big collection
> of desktop background images, and every time I log in, I have a
> script that gives me a different wallpaper image that day. (I use
> hsetroot -center filename to set the wallpaper under openbox; Gnome
> or KDE users might need a different method.) It's fun to see what
> image I'll get each day. For a while I did the same thing with my
> beep tone, so if anything happened to make my computer beep, I
> might hear a crow or a wolf or a grey whale. But it drove my
> husband crazy, so I stopped in the name of marital harmony.
>
import os
import random
filelist = os.listdir(".")
print "randomly chose the file: {0}".format(random.choice(filelist))
3. Make a list of nouns and a list of verbs. Then write a program that
> makes a random sentence by choosing a random noun and a random verb.
>
> You may notice that my verbs are intransitive -- they can be used on
> their own. So you can say "the cat waited", whereas a noun-verb sentence
> wouldn't work if you used a transitive verb like "the cat fixed".
>
> import random
noun = [ "I", "The clock", "The sun", "John"]
intran_verb = [ "ran", "waited", "glowed", "shined" ]
print "{0} {1}".format(random.choice(noun), random.choice(intran_verb))
> 4. (Optional) Change your program to add another sentence type or
> two -- maybe add a list of transitive verbs and make it sometimes
> choose noun-trans verb-noun sentences, other times just
> noun-intrans verb sentences. Or add a list of adjectives and add
> adjectives to your sentences. How would you use random and modulo
> together to say "sometimes do this type of sentence, other times
> do this type"?
>
> The object here is to have fun writing a program that makes
> interesting, wacky sentences.
>
> If you get ambitious and want to spend some more time, try do this
> in a loop, taking input from the user each time, and let the user
> quit by typing q or quit or whatever. You could even ask the user
> for a noun or verb and then make a sentence using it.
>
import random
article = [ "a", "the", "an"]
subject = [ "clock", "sun", "John", "Jane", "baby"]
intran_verb = [ "ran", "waited", "glowed", "delayed" ]
tran_verb = [ "fixed", "played", "hit", "rang" ]
obj = [ "me", "song", "game", "ball", "drum", "bell" ]
adjective = ["beautiful", "stupid", "weak", "kind", "lively" ]
sentence = [
"{0} {1}.".format(random.choice(subject),
random.choice(intran_verb)),
"{0} {1} {2}.".format(random.choice(subject),
random.choice(tran_verb), random.choice(obj)),
"{0} {1} {2} {3}.".format(random.choice(subject),
random.choice(tran_verb), random.choice(adjective),
random.choice(obj)),
"{0} {1} {2}.".format(random.choice(article),
random.choice(subject), random.choice(intran_verb)),
"{0} {1} {2} {3} {4}.".format(random.choice(article),
random.choice(subject), random.choice(tran_verb),
random.choice(article), random.choice(obj)),
"{0} {1} {2} {3} {4} {5}.".format(random.choice(article),
random.choice(subject),
random.choice(tran_verb), random.choice(article),
random.choice(adjective), random.choice(obj)),
]
while (True) :
inp = raw_input("q to quit, enter to continue\n")
if (inp == "q") :
print "quiting"
break
else :
print random.choice(sentence)
More information about the Courses
mailing list