Lab Exercise 5

Introduction

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

Step 1.

Step 2.
Add the statements necessary to produce the output above. For this program, you may assume that the user enters a positive number.

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.

Step 3.
Run your completed program with one number larger than 10. Copy the result from the shell window and paste it in your responses file.

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.

Step 1.
Make a copy of your left_justified.py file and call it right_justified.py.

Step 2.
Modify the program to produce the output above. You may again assume that the user enters a positive number.

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?

Step 3.
Run your completed program with one number larger than 10. Copy the result from the shell window and paste it in your responses file.

Task 3: Accessing the Parts of a String

Let's start doing more with our strings. Today, we will use two new bits of Python

• The len() function returns the length of a string.

• The [] operator returns a subset of the characters in the string. We saw this operator in a limited form back in Session 5. As we saw then, [] starts counting at zero, so:
```    >>> name = 'eugene'
>>> name[0]
'e'
```

Step 1.
Enter this string in the Python shell:
```    >>> warning = 'History! Read it and weep!'
```

Step 2.
For each of the following commands,
• predict what the result will be, then
• enter it into the Python shell to compute the actual result.
```len(warning)
warning[18]
warning[2:8]
warning[2] + warning[17:20] + warning[6]
(warning[0] + warning[22:25]) * 3
```

Step 3.
Create Python expressions to produce these values using warning, [], and concatenation.
```wee ad
```

Task 4: Processing All the Characters in a String

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

Step 1.

Step 2.
Add the statements necessary to produce the output above. Use a for loop to iterate over each character in the user's string to count the number of each vowel: a, e, i, o , and u.

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.

Step 3.
Run your completed program with the input string in the example above. Copy the result from the shell window and paste it in your responses file.

Finishing Up

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.

• left_justified.py
• right_justified.py
• count_vowels.py
• responses.txt

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.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... September 24, 2014