Lab 6 - Loops
Directions for Labs
- Submit your completed lab file online to CourSys (not Canvas). Labs are marked on completion, not correctness, so complete each part to the best of your ability and learn!
- It is recommended that students attend in-person lab sections for lots of help!
- If you would like to attend an in-person lab section, you are welcome to come to any (or more than one) lab section.
- There is no need to attend the section you are enrolled in.
- There is no need to attend any lab sections: there is no attendance taken.
- You can complete the lab on your own time or own computer if you like.
- While completing these labs, you are encouraged to help your classmates and receive as much help as you like. Assignments, however, are individual work and you must not work with another person on assignments.
Print without Line-Feeds
- Inside your
cmpt120
folder, create a new folder forlab6
and make alab6.py
file.- See lab 1 for more detailed directions.
- The
print(...)
statement usually prints out a line-feed (\n
) after every print. However, you can turn that off such as:print("Hello ", end='') print("World")
- Copy this code into your program and try it.
- The
end=''
is called "Keyword Arguments" or "Named Arguments". The first part (end
) specifies which argument we are setting, and the value (''
) gives it a value.
- Write a for-loop which prints the numbers 0 through 9 on a single line.
- Print a message above and below your row of numbers. Only the numbers should be on one line.
-
Hint - use `i`:
You'll need the numbers inside the loop, so use a `for` loop which sets i using `range(...)` -
Hint - one line:
Turn off the line-feed that is added at the end of each of the print statements so it appears on one line. Set `end`. -
Hint - print line-feeds:
Add some regular `print()` calls to print out any line-feeds you need extra. - Sample output:
Demonstrate print() with no line-feeds 0123456789 DONE!
Print with Loops
We are now going to print some BIG block letters to the screen.
- Create two named constants (variables which are given a value and never changed) to store the width and height of the characters we are going to make.
- Name the constants in all upper case so other programmers know they are to be treated as constants.
- Make the width and height about 5 (or a bit larger if you like).
-
Hint: code:
It will look like: WIDTH = 5
- Using three separate loops, write a large C to the screen.
- Do not use string multiplication (like
"C"*5
). This would be a better way to generate the output, but this lab is about playing with loops! -
Hint - 3 loops:
Use one loop for the first horizontal line of C's, a second loop to print the 3 C's on the left, and a third loop to print the final row. -
Hint - line-feeds:
Use the `end` approach above to turn off line-feeds when you want. -
Hint - line-feeds:
You'll want some line-feeds, so consider adding new `print()` statements to give a extra line-feed if needed. - Sample output may look like:
CCCCC C C C CCCCC
- Do not use string multiplication (like
Nested Loops
- Using two nested loops (a
for
loop inside anotherfor
loop) write out a large M.-
Hint - row/col:
The outer loop will loop through the rows, the inner loop will loop through the columns. -
Hint - names:
Name the outer loop's variable `row`, and make it iterate over range `HEIGHT`. The inner loop is for the `col`. -
Hint - line-feed:
In the outer loop (over the rows), make the last thing you do be print a line feed to get ready for the next row. -
Hint - `M` or ` `:
In the inner loop, print a single character each time through. Print an `M` or a ` `. -
Hint - `M` or ` `:
Decide to print an 'M' or a space based on the column and row. If in the first row, print `M`; otherwise if in the first column or the last column, print an `M`; otherwise, if in the "middle" column, print an `M`; otherwise, print a space. -
Hint - Expression:
You can code this using some if-elif-else statements, or you could use a single `if` statement such as:if row == 0 or col == 0 or ...:
-
Hint - Half way:
You can check if you are half-way through the columns with: `if col == WIDTH // 2:` (using integer division) - Sample output
MMMMM M M M M M M M M M M M M
-
Experiment!
- Make your program print out a word of your choice in big block letters! Write the letters one-over the next, like the above "CM".
- Must be at least 6 characters long.
- You must use at least one loop for each character.
- You must not use string multiplication like
'N'*5
(BAD!) - You should experiment with nested loops.
- You should experiment with complex conditions using
or
,and
, and perhapsnot
. - You should experiment with
//
,==
,%
, ... - You should experiment with
if
,if-else
,if-elif-else
.
Challenges (Optional)
- Horizontal letters (Harder!)
- Change your code to write the big-letters horizontally (vs vertically)
-
Hint - Outer loop:
Use one for loop to iterate through the rows. -
Hint - Inner loop:
Inside the for loop, call functions to draw each letter, passing in the row. -
Hint - Space:
Make sure you insert a space or two between each big character.
- Extra-extra challenge (optional): Have the user type in a word and print it out in big letters! You may want to start only supporting a few letters.
- You could do this with vertical letters, or (even harder!) horizontal letters.
Submission
Submit your lab6.py
file to CourSys by Sunday midnight. Remember you can submit up to 3 days late with no penalty if needed (but please plan to get it done on time to stay up to date on the course!)
Topics Covered
for
loops- Nested loops
if
statementsprint()
without linefeeds