The first two tasks focus on loops, in particular one loop nested inside another. The last two tasks focus on strings.
Create a directory on your USB device for this lab, say, lab05, and launch IDLE.
This week, you will not submit your shell window at the end of the session. If you close it accidentally along the way, worry not!
You will submit a responses.txt file this week. Download this template file and use it to record any answers or predictions asked for in the exercises.
We can write programs to produce ASCII art. One of the simplest patterns is a triangle. We might interact with such a program like this:
Enter the height for your triangle: 6 * * * * * * * * * * * * * * * * * * * * *
Notice that there is a space after each star. We need that to make the triangle close to balanced in height and width (isosceles).
You must use a loop to print or build the row of stars. You may use this statement:
print('*', end=' ')or use the string-appending approach we saw in class.
But we may need a triangle pointing the other direction. For example:
Enter the height for your triangle: 6 * * * * * * * * * * * * * * * * * * * * *
We still have a space after each star.
Again, you must use a loop to print or build the row of spaces and stars.
Hint: How many spaces do you have to print instead of stars on the first line? The second line? The nth line?
Let's start doing more with our strings. Today, we will use two new bits of Python
>>> name = 'eugene' >>> name[0] 'e'
>>> warning = 'History! Read it and weep!'
len(warning) warning[18] warning[2:8] warning[2] + warning[17:20] + warning[6] (warning[0] + warning[22:25]) * 3
Record your predictions and actual results in your responses file.
wee ad wee wee adad
Record your expressions in your responses file.
We can process all the characters in a string using a form of the for statement we saw in Chapter 2:
text = '...' for char in text: a suite of statements in which char is bound to each character in text, one at a time
We can use such a loop to count things in a string, for example:
Enter a string: Tiger got to hunt. Bird got to fly. There are 0 a's in the string. There are 1 e's in the string. There are 2 i's in the string. There are 4 o's in the string. There are 1 u's in the string.
You must use a loop to do the counting. You may not use any built-in Python functions for handling strings other than the ones we saw above.
Make sure that your program files are complete and saved. Save your responses.txt file.
Submit your files for grading on the electronic submission system, at lab05 -- Loops and Strings.
As always, make sure you see the verification screen that says The files listed above were uploaded.
If you need any help, let me know.