You are on page 1of 23

CCE20003 PROGRAMMING I

Lecture 2

Spring 2015

Creative Convergence Education


Handong Global University

CCE20003 – Lecture 2 Handong Global University 1


CCE20003 HOMEPAGE

CCE20003 – Lecture 2 Handong Global University 2


HOW TO INSTALL PYTHON - 1
1. Download the Python interpreter and environment
python-2.7.2.msi and install it. On 64-bit
Windows machines, use python-
2.7.2.amd64.msi instead.
- When the installation asks for the Destination
Directory, keep the default C:\Python27\.

- Do not change anything on the customization page.

2. Download the Wing IDE 101 wingide-101-4.1.31.exe


and install it.

CCE20003 – Lecture 2 Handong Global University 3


HOW TO INSTALL PYTHON - 2
3. Download and install the Python imaging library (PIL)
PIL-1.1.7.win32-py2.7.exe. On 64-bit
Windows machines, use PIL-1.1.7.win-amd64-py2.7
instead.

4. Download the zip file cce20003-201500304.zip and


store it on your desktop. Then right-click the file icon,
and select Extract.
- When it asks for a directory, select the directory
C:\Python27\Lib\site-packages.

CCE20003 – Lecture 2 Handong Global University 4


HOW TO INSTALL PYTHON - 3
• You may download the necessary files from Hisnet

CCE20003 – Lecture 2 Handong Global University 5


2D ROBOT CONTROL
Read Sections 5~9 to do following tasks:

Zigzag1*
Hurdles1*
Newspaper delivery
Harvest1
Hurdles2
Harvest2*
Harvest3

CCE20003 – Lecture 2 Handong Global University 6


INTERACTIVE MODE (1/2)

>>>from cs1robot import *


>>>create_world()

CCE20003 – Lecture 2 Handong Global University 7


INTERACTIVE MODE (2/2)

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

CCE20003 – Lecture 2 Handong Global University 8


SCRIPT MODE

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()

CCE20003 – Lecture 2 Handong Global University 9


TRACE & PAUSE
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()

CCE20003 – Lecture 2 Handong Global University 10


BUGS
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()

CCE20003 – Lecture 2 Handong Global University 11


COMMENTS
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!

CCE20003 – Lecture 2 Handong Global University 12


TURNING RIGHT (1/3)

1
3

4
0

CCE20003 – Lecture 2 Handong Global University 13


TURNING RIGHT (2/3)
# initialize the world # move and turn right
from cs1robots import * hubo.move()
create_world() hubo.turn_left()
hubo = Robot() hubo.turn_left() turn right
# turn left hubo.turn_left()
hubo.turn_left() Why? # move and turn right
# move and turn right hubo.move()
hubo.move() hubo.turn_left()
hubo.turn_left() hubo.turn_left() turn right
hubo.turn_left() turn right hubo.turn_left()
hubo.turn_left() hubo.move()

CCE20003 – Lecture 2 Handong Global University 14


TURNING RIGHT (3/3)
from cs1robots import cre- # turn right and move
ate_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()
CCE20003 – Lecture 2 Handong Global University 15
PRACTICE USING FUNCTIONS 1
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.

CCE20003 – Lecture 2 Handong Global University 16


PRACTICE USING FUNCTIONS 2

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.

CCE20003 – Lecture 2 Handong Global University 17


HOW TO LOAD HURDLES1
from cs1robots import * load_world("worlds/
hurdles1.wld") #but not create_world()
hubo = Robot()

directory hurdle1 file

Create a directory worlds and download all world files.

CCE20003 – Lecture 2 Handong Global University 18


DROPING A BEEPER

from cs1robots import load_world("worlds/


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

CCE20003 – Lecture 2 Handong Global University 19


PICKING A BEEPER

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()

CCE20003 – Lecture 2 Handong Global University 20


PRACTICE USING FUNCTIONS 3
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 previous


lecture, decompose the problem into sub-problems and
then focus on each sub-problem one by one.
CCE20003 – Lecture 2 Handong Global University 21
PRACTICE USING FUNCTIONS 4
PROBLEM 4: HARVEST1
It's harvest time! Make the robot pick up all the carrots
(represented by beepers) in this garden. The world file is
harvest1.wld. Employ top-down design. Use while-loop.

CCE20003 – Lecture 2 Handong Global University 22


PRACTICE WITH FOR-LOOPS
PROBLEM 5 : HURDLES2*
Do Hurdles1 using for-loops.

PROBLEM 6: HARVEST2
Do Harvest1 using for-loops.

PROBLEM 7: HARVEST3
Load “worlds/harvest2.wld”
Pick up all beepers in this map?

CCE20003 – Lecture 2 Handong Global University 23

You might also like