0% found this document useful (0 votes)
8 views

Lecture3 Note

Uploaded by

abelgetaneh201
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture3 Note

Uploaded by

abelgetaneh201
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

AST

CSEg 1101 Introduction to Computing


Lecture 3

Week 2

Fall 2022

Computer Science and Engineering


School of EE & Computing
Adama Science & Technology University
REVIEW AST
U
Characteristics of Python

Instruction set
Arithmetic and logical operations
+, -, *, /, and ** for defining
and, or, not expressions
Assignment
Conditionals
Iterations
Input/output

No pointers
No declarations

CSE 1101 2
AST
U

CSE 1061 3
OUTLINE AST
U
Conditionals
Iterations
for_loops
while_loops

Reading assignment of the text book :


Chapter 4 Conditionals and recursion
Chapter 6 Iteration

CSE 1061 4
CONDITIONALS AST
U

if <condition> :

True block

else:
False block

<condition> has a “True” or “False” value, representing true or


false, respectively.

If it is true, the True block is executed; otherwise, the False


block is executed.
CSE 1061 5
AST
U

What will be printed ?


if True:
print “CCE20003 is my favorite course"
else:
print "Every CCE20003 student will receive an A+"

Now, do you understand?


if 5 > 3:
print " CCE20003 is my favorite course“
else:
print "Every CCE20003 student will receive an A+“

Now, what will happen?


if False:
print “Every CCE20003 student will receive an A+“

CSE 1061 6
AST
U

The keyword not inverts the sense of the condition: not


True is False, and not False is True.

What is the output?


>>print not 3 < 5

CSE 1101 7
SENSING A BEEPER AST
U

We want Hubo to make 9 steps and pick all beepers on


the way. However, we do not know where beepers are. If
there is no beeper at a grid point, then
hubo.pick_beeper() causes an error.

How to sense a beeper?


Use hubo.on_beeper()

CSE 1061 8
AST
U

Move and pick a beeper if def move_and_pick():


any. hubo.move()
Take a step forward. if hubo.on_beeper():
Check if there is a hubo.pick_beeper()
beeper.
If yes, pick it up. for i in range(9):
move_and_pick()
Move forward 9 steps.
At each step, move No False block!!
and pick up a
beeper
if any.
CSE 1061 9
AST
U
Let’s do the opposite: we want to drop a beeper, but only
if there is no beeper at the current location.

if not hubo.on_beeper():
hubo.drop_beeper()

Ex) Program: “0 move_and_pick.py”

CSE 1061 10
FOLLOWING THE BOUNDARY AST
U

Hubo tries to follow the boundary of the world: He moves


forward if there is no wall; otherwise, turn to the left.

from cs1robots import *


create_world(avenues =5, streets = 5)
hubo = Robot()
hubo set_trace(”blue”)

def move_or_turn():
if hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()

for i in range(20): Why 20 ?


move_or_turn()
CSE 1061 11
MOVING AND DANCING AST
U
hubo =Robot(beeper=5)

def dance():
for i in range(4):
hubo.turn_left()

def move_or_turn():
if hubo.front_is_clear():
dance()
hubo.move()
else:
hubo.turn_left()
hubo.drop_beeper()

For i in range(18):
move_or_turn() ## Ex: 1
dancing.py CSE 1061 12
MULTIPLE CHOICES AST
U

elif combines else and if to express many alternatives


without complicated indentation.

if hubo.on_beeper():
hubo.pick_beeper()
else:
elif hubo.front_is_clear():
if hubo.front_is_clear():
hubo.move()
hubo.move()
elif hubo.left_is_clear(): else:
hubo.turn_left() if hubo.left_is_clear():
elif hubo.right_is_clear(): hubo.move()
turn_right() ---------------------------
else:
turn_around()

CSE 1061 13
MULTIPLE CHOICES AST
U

elif combines else and if to express many alternatives


without complicated indentation. Yes
on_beeper
= TRUE ?
if hubo.on_beeper(): No hubo.pick_beeper()
hubo.pick_beeper()
front_is_clear Yes
elif hubo.front_is_clear(): = TRUE ?
hubo.move() No hubo.move()

elif hubo.left_is_clear(): left_is_clear Yes


= TRUE ?
hubo.turn_left()
No hubo.turn_left()
elif hubo.right_is_clear():
right_is_clear Yes
turn_right() = TRUE ?
else: No turn_right(
turn_around() )
turn_around
()
CSE 1061 14
WHILE-LOOPS AST
U
while <condition>:
block

The block is executed as long as <condition> is True;


otherwise, it is skipped.

CSE 1061 15
AST
U

A while-loop repeats instructions as long as a condition is


true.
while not hubo.on_beeper(): Move forward as long
hubo.move() as there is no beeper

A for-loop repeats some instructions a fixed number of times.


for i in range(9):
hubo.move()

CSE 1061 16
AST
U

Let’s write a program to let the robot walk around the


boundary of the world until he comes back to the
starting point
Think your solution for this problem 🡪
your method 🡪
your Algorithm for solving a problem !!

1. Put down a beeper to mark the


starting point
2. Repeat steps 3 and 4 while no
beeper
found
3. If not facing a wall, move
forward
4. Otherwise, turn left
5. Finish
CSE 1061 when we found the 17
AST
U

hubo.drop_beeper()
hubo.move() Why this?
while not hubo.on_beeper():
if hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
hubo.turn_left()

Does this program always work?


Well, …..

CSE 1061 18
AST
U
How about this case?
hubo.drop_beeper()
hubo.move()
while not
hubo.on_beeper():
if
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
hubo.turn_left()

Try the code in the previous


page with ”amazing2.wld” and
see if the previous code works.
Sometimes we need a right 19
CSE 1061
turn!
AST
U

Does this work?


Well, ………….
hubo.drop_beeper()
hubo.move()
while not hubo.on_beeper():
if
hubo.right_is_clear():
turn_right()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
hubo.turn_left()
Infinite loop!
CSE 1061 20
AST
U

How about this?

hubo.drop_beeper()
hubo.move()
while not hubo.on_beeper():
if hubo.right_is_clear():
turn_right()
hubo.move()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
Does this always
CSE 1061 work? 21
AST
U

How about this


case?
hubo.drop_beeper()
hubo.move()
while not hubo.on_beeper():
if hubo.right_is_clear():
turn_right()
hubo.move()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()

CSE 1061 22
AST
U

Does this work? hubo.drop_beeper()


while not
hubo.front_is_clear():
hubo.turn_left()
hubo.move()
while not hubo.on_beeper():
if hubo.right_is_clear():
turn_right()
hubo.move()
elif
hubo.front_is_clear():
hubo.move()
else:
hubo.turn_left()
Does this always work?

Hubo.turn_left()
CSE 1061 23
AST
U

Still not perfect !


Very sensitive to the initial position of
Hubo.

CSE 1061 24
AST
U

hubo.drop_beeper()
while not
hubo.front_is_clear(): def
hubo.turn_left() mark_and_move():
hubo.move()

while not
hubo.on_beeper(): def follow_wall():
if hubo.right_is_clear():
turn_right() mark_and_move()
hubo.move()
while not
elif
hubo.on_beeper():
hubo.front_is_clear():
hubo.move() follow_wall()
else: hubo.turn_left()
hubo.turn_left()
Hubo.turn_left() CSE 1061 25
COMMENTS FOR HUMANS AST
U
One of the secrets of writing good, correct, elegant
programs is to write them as if you wrote them for a
human reader, not a computer. Let’s clean up our
program.

How ? By adding comments !

CSE 1061 26
AST
U

“””
This program lets the robot go around his world counter-
clockwise, stopping when he comes back to his starting point.
“””
#Turn right.
def turn_right():
for i in range(3):
hubo.turn_left()
#Mark the starting point and move
def mark_and_move():
hubo.drop_beeper()
while not hubo.front_is_clear():
hubo.turn_left()
hubo.move()

(continued)
CSE 1061 27
AST
U

#Follow the wall at each iteration.


def follow_wall():
if hubo.right_is_clear():
# turn right to follow the wall
right_turn()
hubo.move()
elif hubo.front_is_clear():
# move forward while following the wall
hubo.move()
else:
# turn left to follow the wall
hubo.turn_left()
(continued)

CSE 1061 28
AST
U
#Begin actual move.
mark_and_move()
#Follow the entire wall.
while not hubo.on_beeper():
follow_wall()
Hubo.turn_left()

CSE 1061 29
STEPWISE REFINEMENT AST
U
1. Start with a primitive program that solves a simple
problem.
2. Make small changes, one at a time to generalize the
program.
3. Make sure that each change does not invalidate
what you have done before.
4. Add appropriate comments (not just repeating what
the instruction does).
5. Choose descriptive names.

CSE 1061 30

You might also like