0% found this document useful (0 votes)
52 views19 pages

CS101 Lab 03

The document provides an overview of the tasks for Week 3 of CS101, which include using conditionals and while loops to have Hubo return to its starting position, collect trash, and close windows, as well as using the cs1media module to convert an image into a 3-color poster; it reviews functions from previous weeks and introduces new functions like loading images before describing the tasks in detail.

Uploaded by

chaosofdoomx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views19 pages

CS101 Lab 03

The document provides an overview of the tasks for Week 3 of CS101, which include using conditionals and while loops to have Hubo return to its starting position, collect trash, and close windows, as well as using the cs1media module to convert an image into a 3-color poster; it reviews functions from previous weeks and introduces new functions like loading images before describing the tasks in detail.

Uploaded by

chaosofdoomx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Welcome !

CS101 Introduction to Programming


Week 3
Last step with Hubo & Introduction to cs1media
(Obje cts)
A quick review
Functions we’ve seen
▪ Creating a new world
✓ Be fore cre ating a world, re me mbe r to import
ne ce ssary module s
from cs1robots import *
create_world()
✓ The n,
hubo = Robot()

▪ Cre ate a robot name d ‘hubo’


✓ Cre ate a de fault robot
hubo = Robot(beepers = 10)
✓ Cre ate a robot with 10 be e pe rs
Functions we’ve seen
▪ Drop a beeper
hubo.turn_left()
hubo.move()
hubo.drop_beeper()
hubo.move()

▪ Pick a beeper
for i in range(2):
hubo.turn_left()
hubo.move()
hubo.pick_beeper()
hubo.move()

✓ Before picking up a beeper, hubo should be on a beeper!


Functions we’ve seen
▪ Can check if there is a wall on each of the three sides
hubo.front_is_clear()
hubo.left_is_clear()
hubo.right_is_clear()

hubo.left_is_clear() == True
hubo.front_is_clear() == False
hubo.right_is_clear() == True
If statements
▪ If statements sequentially checks the conditionals
if conditional_expression_1:
works to do when conditional_expression_1 evaluates to True

elif conditional_expression_2:
works to do when conditional_expression_1 evaluates to False &
conditional_expression_2 evaluates to True

elif conditional_expression_3:
works to do when conditional_expression_1 evaluates to False &
conditional_expression_2 evaluates to False &
conditional_expression_3 evaluates to True

else:
works to do when all the above conditions are False
While loops
▪ while statement loops until the conditional evaluates to
true
while conditional_expression:
works to do while conditional_expression evaluates to True

▪ Be careful!
Iamlying = False

while (Iamlying == False) :


print (“cs101 is so much fun”)
Iamlying = True
New functions !!
New functions
▪ Load an image file
✓ Be fore loading an image file , re me mbe r to
import ne ce ssary module s
from cs1media import *
img = load_picture("img/a.png" )
✓ The n,
img.show()
to se e the image .
Convert image to be Black & White
▪ Use img.get(x, y) and img.set(x, y, color) to retrieve and assign pixel color
from cs1media import *

threshold = 100
white = (255, 255, 255)
black = (0, 0, 0)

img = load_picture('./images/image.png')
w, h = img.size()
for y in range(h):
for x in range(w):
r, g, b = img.get(x, y)
v = (r + g + b) // 3 # average of r, g, b
if v > threshold:
img.set(x, y, white)
else:
img.set(x, y, black)
img.show()
Week 3
Today’s Tasks
Tasks for Today!
▪ Read sections 18~20 in the robot notes

Four re lative ly hard tasks


▪ Re turn
▪ Trash1
▪ Trash2
▪ Rain
And one simple task for introduction to cs1me dia
▪ 3 Color Poste r

▪ Whe n you have comple te d all the tasks, le t a TA mark you off
Task 1 | Conditionals, While-loop - Return
▪ Write a program that moves Hubo to his usual starting position
(Ave nue 1, Stre e t 1, facing East)
○ Initialialize d position : any position and orie ntation in an e mpty world
■ (e .g.) hubo = Robot(orie ntation ='W', ave nue = 7, stre e t = 5)
○ Use hubo.facing_north() to che ck dire ction of Hubo’s face

Before After
Task 2 | Conditionals, While-loop – Trash 1
▪ Hubo wants to collect all the litter, and put it in the garbage can
○ Use hubo.carrie s_be e pe rs() to put litte r to trash
NOTE: Your program must work for both trash1.wld and trash2.wld

trash1.wld

trash2.wld

Before After
Task 3 | Conditionals, While-loop – Trash 2
▪ Hubo wants to collect all the litter in the backyard of his house and bring it
back to his starting position
▪ Your solution should not depend on the exact location of the garbage ,
nor should it depend on the size of the yard
NOTE: Your program must work for trash3.wld and trash4.wld
HINT: Zigzag2 + Re turn

Before After
Task 4 | Conditionals, While-loop – Rain
▪ Help Hubo close all the windows in Ami’s house
▪ Drop a be e pe r in front of e ach window
▪ Le t Hubo start at (2,6) with sufficie nt be e pe rs.
■ e .g., hubo = Robot(be e pe rs = 10 , ave nue = 2, stre e t = 6, orie ntation = "E")
NOTE: Your program must work for both rain1.wld and rain2.wld

Before After
Task 5 | cs1media - 3 Color Poster
▪ Write a program that converts an image file into a three-color poste r
▪ Conve rt pixe ls with very bright color to yellow
▪ Conve rt pixe ls with very dark color to blue
▪ Conve rt all othe r pixe ls to be green
NOTE: Start with black & white photo ge ne rating code .
Modify the code to conve rt image file s into thre e -color poste rs.

Before After
questions?

You might also like