0% found this document useful (0 votes)
12 views22 pages

Lecture02 Note

Uploaded by

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

Lecture02 Note

Uploaded by

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

CSEg 1101 Introduction toASTU

Computing
Lecture 2 + HW#1

Fall 2022

Computer Science and Engineering


School of EE & Computing
Adama Science & Technology University

CSE 1061 – Lecture Introduction to Computing 1


2D ROBOT CONTROL ASTU

Read Sections 5~9 to do following tasks:

Zigzag1*
Hurdles1*
Newspaper delivery
Harvest1
Hurdles2
Harvest2*
Harvest3

CSE 1061 – Lecture Introduction to Computing 2


INTERACTIVE MODE (1/2) ASTU

>>>from cs1robot import * # prompt >>>


>>>create_world() # Creates 2D grid ma-
trix
for Robot World

CSE 1061 – Lecture Introduction to Computing 3


INTERACTIVE MODE (2/2) ASTU

>>>hubo = Ro- >>>hubo.- >>>hubo.turn_l


bot() move() eft

CSE 1061 – Lecture Introduction to Computing 4


SCRIPT MODE ASTU

from cs1robots import *


create_world()
hubo = Robot()
hubo.move()
hubo.turn_left()
hubo.move()
hubo.turn_left()
hubo.move() Why ?
hubo.turn_left()
hubo.move()
hubo.turn_left()

CSE 1061 – Lecture Introduction to Computing 5


TRACE & PAUSE ASTU

from cs1robots import *


create_world()
hubo = Robot()
hubo.set_trace(‘blue”)
hubo.set_pause(2)
hubo.move()
hubo.turn_left()
hubo.move()
hubo.turn_left()
hubo.move()
hubo.turn_left()
hubo.move()
hubo.turn_left()

CSE 1061 – Lecture Introduction to Computing 6


BUGS ASTU

Hubo.mMove() Syntax error !

from cs1robots import *


create_world()
hubo = Robot()
hubo.move()
hubo.turn_left() Runtime error !
hubo.turn_left() What’s wrong ?
hubo.turn_left()
hubo.move()

CSE 1061 – Lecture Introduction to Computing 7


BUGS ASTU

Hubo.mMove() Syntax error !

from cs1robots import *


create_world()
hubo = Robot()
hubo.move()
hubo.turn_left()
hubo.turn_left()
hubo.turn_left()
hubo.move()

CSE 1061 – Lecture Introduction to Computing 8


BUGS ASTU

Hubo.mMove() Syntax error !

from cs1robots import *


create_world()
hubo = Robot()
hubo.move()
hubo.turn_left()
hubo.turn_left()
hubo.turn_left()
hubo.move()

CSE 1061 – Lecture Introduction to Computing 9


BUGS ASTU

Hubo.mMove() Syntax error !

from cs1robots import *


create_world()
hubo = Robot()
hubo.move()
hubo.turn_left() Runtime error !
hubo.turn_left() What’s wrong ?
hubo.turn_left()
hubo.move()

CSE 1061 – Lecture Introduction to Computing 10


COMMENTS ASTU

• Used for other humans inside a program


-To embed programmer-readable annotations.
-To make the source code easier to understand.
• Starting with #
• Ignored by the Python interpreter:

# My first program
from cs1robots import *
create_world()

# This line should be ignored!

CSE 1061 – Lecture Introduction to Computing 11


TURNING RIGHT (1/3) ASTU

1
3

4
0

CSE 1061 – Lecture Introduction to Computing 12


TURNING RIGHT (2/3) ASTU

# initialize the world # move and turn right


from cs1robots import * hubo.move()
create_world() hubo.turn_left()
hubo = Robot() hubo.turn_left() turn
# turn left right

hubo.turn_left() Why? hubo.turn_left()


# move and turn right # move and turn right
hubo.move() hubo.move()
hubo.turn_left() hubo.turn_left()
hubo.turn_left() turn hubo.turn_left() turn
right right

hubo.turn_left() hubo.turn_left()
hubo.move()
CSE 1061 – Lecture Introduction to Computing 13
TURNING RIGHT (3/3) ASTU

from cs1robots import # turn right and move


create_world() hubo.move()
hubo = Robot() turn_right()
def turn_right():
hubo.turn_left() # turn left and move
hubo.turn_left() hubo.move()
hubo.turn_left() turn_right()

hubo.turn_left() hubo.move()
# turn right and move
hubo.move()
turn_right()
CSE 1061 – Lecture Introduction to Computing 14
PRACTICE USING FUNCTIONS 1 ASTU

PROBLEM 1: ZIGZAG1*
Write a program zigzag.py that makes your robot
visit the entire world in a zigzag fashion.

Analyze the zigzag path and


use the functions to make
your program as compact as
possible.

CSE 1061 – Lecture Introduction to Computing 15


PRACTICE USING FUNCTIONS 2 ASTU

PROBLEM2: HURDLES1*
Hubo has entered a hurdles race with obstacles as
in the world file hurdles1. Write a program that
makes Hubo follow the path indicated below in his
way to picking up a beeper, which is at grid(10,1).

: the starting point

Again, make your program as compact as possible


using functions.

CSE 1061 – Lecture Introduction to Computing 16


HOW TO LOAD HURDLES1 ASTU

from cs1robots import *


load_world("worlds/hurdles1.wld") #but not cre-
ate_world()
hubo = Robot()

directory hurdle1 file

Create a directory worlds and download all world files.

CSE 1061 – Lecture Introduction to Computing 17


DROPING A BEEPER ASTU

from cs1robots import load_world("worlds/hur-


dles1.wld")
hubo = Robot(beepers = 1)
hubo.set_trace("blue")
hubo.move()
hubo.move()
hubo.drop_beeper()
hubo.move()
hubo.move()

CSE 1061 – Lecture Introduction to Computing 18


PICKING A BEEPER ASTU

from cs1robots import *


load_world("worlds/hurdles1.wld")
hubo = Robot(beepers = 1)
hubo.set_trace("blue")
hubo.move()
hubo.move()
hubo.pick_beeper()
hubo.move()
hubo.move()

CSE 1061 – Lecture Introduction to Computing 19


PRACTICE USING FUNCTIONS 3 ASTU

PROBLEM 3: NEWSPAPER DELIVERY


Hubo delivers newspapers in his local neighborhood.
Make him climb the stairs to the front door of a house,
drop the newspaper (represented by a beeper) on the top
step, and return to his starting point as illustrated below.
The world file is newspaper.wld.

Use top-down design: As already explained in the pre-


vious lecture, decompose the problem into sub-problems
and then focus on each sub-problem one by one.

CSE 1061 – Lecture Introduction to Computing 20


PRACTICE USING FUNCTIONS 4 ASTU

PROBLEM 4: HARVEST1
It's harvest time! Make the robot pick up all the car-
rots (represented by beepers) in this garden. The
world file is harvest1.wld. Employ top-down design.
Use while-loop.

CSE 1061 – Lecture Introduction to Computing 21


PRACTICE WITH FOR-LOOPS ASTU

PROBLEM 5 : HURDLES2*
Do Hurdles1 using for-loops.

## Homework 1 for Week 1:


## Write a program which does
## the following problem 6 and 7:
## due date: 1 week from next Lab.
## Submition method:
- your source program-file (with your id & name)
HW1-1) PROBLEM 6: HARVEST2
- Do Harvest1 using for-loops.

HW 1-2) PROBLEM 7: HARVEST3


- Load “worlds/harvest2.wld”
- Pick up all beepers in this map?
CSE 1061 – Lecture Introduction to Computing 22

You might also like