# Week I: Lecture Notes ## Agenda - Introductions - Hospital Karel - Future Directions ## Notes Put anything here! - Can spill over 40 minutes ### IDE - Pycharm - Other ### Send out Handouts - For now, lets use [the web](https://compedu.stanford.edu/codeinplace/v1/#/karel/section1/hospital) ## Pseudocode ### Main Tasks - [x] Build Hospitals - [x] Build a hospital -> function - [x] Find beepers -> conditions - Or atleast be aware of them - [x] Reset -> function (with conditions) - Movement Helpers - [x] Turn right -> function - [x] World traversal #### Turn Right - First attempt ```python def turn_right(): turn_left() turn_left() turn_left() ``` - Production go ```python def turn_right(): for i in range(3): turn_left() ``` ##### Bonus (with while) - Make it shorter with loops #### World Traversal ##### For - Hit an error at the end - Manually counting squares ```python for i in range(200): move() ``` - Such a large number means we should probably use a while - We **do not** recognize the end state ```python def main(): for i in range(200): if (front_is_clear()): move() else: turn_right() # Does funny things ``` ##### While - We encode the **end state recognition** ```python def main(): while(front_is_clear()): move() ``` ##### Find Beepers - Is true if karel is on a beeper ```python def main(): while(front_is_clear()): move() if (beepers_present()): turn_left() ``` ##### Building Hospitals - Start to build - Can we use for? We now realize we need ```python turn_left() for i in range(2): move() put_beeper() ``` ###### Two Ways - Is a hospital 2 columns of 3 beepers? - Is a hospital 2 beepers up and 3 down? ##### Build Column 1 ```python def build_column1(): turn_left() for i in range(2): move() put_beeper() ``` #### Build Column 2 ```python def build_column2(): put_beeper() for i in range(2): move() put_beeper() ``` When we need a function to exist for the program to run but haven't defined it yet. ```python def i_exist(): pass ``` ### Alternative 2 columns, 3 beepers high If you're at a beeper, pick it up. #### Goto Bottom ```python def goto_bottom(): for i in range(4): turn_left() if(facing_south()): while(front_is_clear()): move() ``` The whole function: ```python def build_hospital(): pick_beeper() turn_left() build_column2() reset() move() turn_left() build_column2() reset() ``` # Full code ```python from karel.stanfordkarel import * # File: hospital.py # ----------------------------- # Here is a place to program your section problem def main(): while(front_is_clear()): move() if (beepers_present()): build_hospital_2col() def build_hospital_colreset(): pick_beeper() turn_left() build_column2() reset() move() turn_left() build_column2() reset() def build_hospital_2col(): build_column1() turn_right() move() turn_right() build_column2() turn_left() def build_column1(): turn_left() for i in range(2): move() put_beeper() def build_column2(): put_beeper() for i in range(2): move() put_beeper() def reset(): goto_bottom() turn_right() def goto_bottom(): for i in range(4): turn_left() if(facing_south()): while(front_is_clear()): move() def turn_right(): for i in range(3): turn_left() ```