Class 11, Friday, September 15
Practice makes perfect
An optimist thinks — Elevator close buttons — Really do something
Look both ways
Your superpower
- It is time to reveal it
Program chatter.py
- The program generates random sentences based on words provided by the user.
- Steps to be carried out
- Prompt for a word and use the word as the seed for random generation
- Prompt for adjectives and create a list of adjectives by splitting up the user reply.
- Prompt for nouns and create a list of nouns by splitting up the user reply.
- Prompt for verbs and create a list of verbs by splitting up the user reply.
- Use the random module
choice()
capability to generate a sentence of form
w1 w2 w3 w4 w5
where w1 is a random adjective, w2 is a random noun, w3 is a random verb, w4 is a random adjective, and w5 is a random noun. You are to choose the parts of the sentence in order: w1, w2, w3, w4, and then w5.
- Print sentence w1 w2 w3 w4 w5
- The functions and methods in the module writeups will probably help (especially
str
methodsplit()
, andrandom
functionsseed()
andchoice()
.
- You may assume the user types at least one word after each prompt.
Four sample runs (your output should be the same as we are setting the seed each run)
Enter seed: almond
Enter adjectives: teal gorgeous silly
Enter nouns: fish socks grapes noses
Enter verbs: eats runs swims dislikes likes
gorgeous socks swims silly noses
Enter seed: chestnut
Enter adjectives: teal gorgeous silly
Enter nouns: fish socks grapes noses
Enter verbs: eats runs swims dislikes likes
silly noses likes teal noses
Enter seed: walnut
Enter adjectives: big small round flat yellow purple
Enter nouns: cattle sheep cookies chickens bugs stoves
Enter verbs: run love use smell are
yellow bugs smell purple cattle
Enter seed: pecan
Enter adjectives: big small round flat yellow purple
Enter nouns: cattle sheep cookies chickens bugs stoves
Enter verbs: run love use smell are
purple cookies are yellow chickens
Program digit_box.py
- Get an integer n from the user.
- Print an n-by-n grid of one-digit numbers.
- The digit in the r th row, c th column is to be the last digit of the product r × c.
Two possible program runs
Type an integer: 3
1 2 3
2 4 6
3 6 9
Type an integer: 12
1 2 3 4 5 6 7 8 9 0 1 2
2 4 6 8 0 2 4 6 8 0 2 4
3 6 9 2 5 8 1 4 7 0 3 6
4 8 2 6 0 4 8 2 6 0 4 8
5 0 5 0 5 0 5 0 5 0 5 0
6 2 8 4 0 6 2 8 4 0 6 2
7 4 1 8 5 2 9 6 3 0 7 4
8 6 4 2 0 8 6 4 2 0 8 6
9 8 7 6 5 4 3 2 1 0 9 8
0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 0 1 2
2 4 6 8 0 2 4 6 8 0 2 4
Observations
- The last digit of a base 10 number equals the remainder of that number when divided by 10. There is a Python operator for that.
- Since the number of rows n is user-provided, a loop is necessary to process each row of the digit table.
- Since the number of columns per row also comes from n, a loop is necessary to process each row.
- Because the column processing happens for every row, the loop to process the rows will contain a loop to process the columns for a row; i.e., nested looping.
To do
- Review artifacts