# Lab 05 : Stars and Triangles

### Objectives:

• Additional practice with looping and conditionals

### Introduction

Once again, this week I will ask you to work with a partner of your choice.  You will turn in a SINGLE answer sheet for both partners.  As a reminder, you should...

• Be working together
• Only be working at ONE computer
• Both people should work and you should share duties.  For example, one partner runs the computer for activity A while the other partner tells him/her what to do and writes any answers on the "answer" sheet.  When you reach the end of Activity A you should change roles.

Failure to follow these guidelines may cause you to lose points for this activity.

# Activity  A : Printing out a simple sequence

Enter the following code into an editor in python.

Run this code.

[Q1]  What happens?

Modify the previous code to look like this instead:

Run this code.

[Q2]  How is the output from this code different than the output from the previous code?

# Activity  B : Printing out a row of stars

We are going to play with creating a type of art today called ASCII art. This type of computer art consists of arranging printable characters on the screen in an artistic form. (Think of creating art and shapes on the screen before modern graphics cards.)

The first shape we make today is a simple row of stars.

Write a program that you will save in a file called starLine.py.

This program should:

• Ask the user for a non-negative integer, which is the total number of stars
• If the number is invalid, keep prompting the user until a valid number is given
• Print out the total number of stars in a row, where each star is seperated by a single space
• You must use a loop! (Do not use string multiplication.)

For example:

Note: Your code needs to have the following line in it somewhere:

(This line will print one star at a time, followed by a space. It also allows the next print to start on the same line.)

Note 2: You cannot use string manipulation to get your stars -- you must practice looping and printing one star at a time in this lab. So, this mean I will not accept the following code in any activity or any permutation of this code where you use multiplication to print your stars:

Note 3: While you might be tempted to use a while loop, a for loop is actually easier in the long run....

Save this code as starLine.py and upload it into eLearning:

• Go to this class
• Click on the "Lab05" folder
• Upload the files to their proper locations.

[SIG1]  Please show starLine.py to an instructor BEFORE moving on

# Activity  C : Printing out a left-justified right triangle

Modify your previous code to use nested loops so that it prints out mulitple lines of stars, which happen to look like a left-justified right triangle! Write a loop around your previous loop code to run it multiple times with different numbers of stars. Your code should prompt for a positive height, and re-prompt if a positive number was not entered.

For example:

Hint 1: The height is the number of times the outer loop should run.

Hint 2: The inner loop prints out one line of stars, starting with one star on the first line and incrementing the number of stars by one each time it runs.

Save this code as leftTriangle.py and upload it into eLearning:

• Go to this class
• Click on the "Lab05" folder
• Upload the files to their proper locations.

[SIG2]  Please show leftTriangle.py to an instructor BEFORE moving on

# Activity  D : Printing out a right-justified right triangle

In Activity C, you printed out a left-justified right triangle. Now I want you to think about writing a right-justified right triangle! For example:

Each line is actually a mixture of spaces and stars.

[Q3] How does the height of the triangle relate to the number of spaces and stars printed the first line? On the second line?

Save this code as rightTriangle.py and upload it into eLearning: