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

Leslie leslie.brothers at verizon.net
Sun Jul 17 16:47:37 UTC 2011

Lots of fun!!
> ====================== Homework =============================
> 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?

I couldn't quite get how to use the hint, because it seems the modulo
result depends on BOTH x and y.  I will see how others used this hint.
In the meantime, I fixed it by setting a condition on the modulo result:

#ex 1
colors = [ 'red', 'orange', 'yellow', 'green', 'blue' ]
patch = 0
for i in range(1,5):
	x = 20
	while (20 % x) < 5:
		patch += 1
		print colors[(20 % x)],"\t","- patch no. ",patch
		x -= 1

> 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.

import os
import random

print random.choice(os.listdir("/usr/share/backgrounds/images"))

> 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.
I made 2 classes of nouns (animate and inanimate) and 2 corresponding
classes of verbs, since they match better that way. See below.
> 4. (Optional)  Change your program to add another sentence type or
>    two -- 
I threw in some adjectives.  I didn't pull out the full transformational
grammar recipes! Oops, on re-reading the question, I see I forgot to try
using modulo.
I'll go ahead and submit what I have, then fool around with modulo on my
>    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

while True:
	choice = raw_input("\nType 'go' if you want to see a random
sentence.\nOr, enter a name if you want a random sentence using that
name.\nType 'q' to quit.\n")
	if choice == "q":
		print "Bye"

	verbsan = [ 'cogitated', 'flipped out', 'vegetated', 'procrastinated',
'ruminated', 'zoned out' ]
	verbsinan = [ 'mouldered', 'exploded', 'gyrated', 'vibrated' ]
	nounsan = [ 'Fred', 'Pablo Escobar', 'My aunt', 'Dixie' ]
	nounsinan = ['The moon', 'A water balloon', 'The traffic lights on
Sunset' ] 
	adjs = [ 'deadly', 'pointless', 'incomprehensible', 'tragic',
'awesome' ]

	verbs = [ verbsan, verbsinan ]
	nouns = [ nounsan, nounsinan ]

	if choice == 'go':
		if random.choice(nouns) == nouns[0]:
			print "\n",random.choice(nounsan), random.choice(verbsan)+".","It
was", random.choice(adjs)+"."
			print "\n",random.choice(nounsinan), random.choice(verbsinan)+".",
"It was", random.choice(adjs)+"."
		print "\n",choice, random.choice(verbsan)+".","It



More information about the Courses mailing list