Session Thirteen

advertisement
More Nested Loops and Lab 5
Intro to Computer Science
CS1510, Section 2
Dr. Sarah Diesburg
Yesterday’s Lab

Activity A


What gave you the most trouble in Activity B?


New ways to print on the same line
How did you address this?
What gave you the most trouble in Activity C?
New ways to print
# Playing with print endings
x = 0
while x<10:
print(x, end=‘ ’)
x=x+1
print()
print(“Final value of x is”,x)
3
What code could we write to print a
triangle?
*
**
***
****
*****
What code could we write to print
a triangle?

First we need to figure out how to print one
line of stars of varied length

Next, we need to do this over and over
5
Important things

Name your variables descriptive names!




x, y, z, a, b, c don’t make much sense with nested
loops
Once you figure out what you need to do,
write a detailed comment about it
Start by solving a small problem, then work
up to larger problems
If all else fails, step through the program line
by line
6
What about a right-justified
triangle?
*
* *
* * *
* * * *
*
*
*
*
*
7
What about a right-justified
triangle?

We need to print spaces before the stars

To solve the problem, we need to think about
2 things


Number of spaces
Number of stars
8
Download