14

Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

  • Upload
    buinhi

  • View
    231

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling
Page 2: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

Our First Script

Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter

To run it type

Page 3: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

Controlling the robot In your shared folder there’s a script called robot.py When this is run it will drive the robot forward for 5 seconds. The commands to move the robot are robot.forward() robot.stop() robot.backward() robot.right() robot.left() The sleep function is used to control the amount of time the robot spends on each movement. sleep()

Page 4: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

Today’s Challenges

Obstacle course

Page 5: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

Last week we moved our robots by an amount of seconds but it would be a lot simpler if we could use

say Centimetres

Just as Python comes with pre-written functions we can also write our own functions

The advantages of using our own functions are • The program is easier to understand. • You can reuse code easily. • Easier to design and test code.

Page 6: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

What do functions do 1. It can take in Data (called Arguments) 2. It can perform some computational task. 3. It can return a result and/or modify parameters.

A function's definition is where we write the code for our function has to come before you call the function. We call our own functions the same way we call Python’s built in functions.

Page 7: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

Writing A Function The def statement

The def statement is made up of the def keyword, followed by a function name with parentheses, and then a colon (the : sign). There is a block after the statement called the def-block. The code in this block is executed when we call the dist_to_time() function later in the program.

Page 8: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

Our def Block

Page 9: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling

Return

This is the return keyword, which only appears inside def-blocks. It will send values back to our main program. Functions with return values are normally called like this

Page 10: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling
Page 11: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling
Page 12: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling
Page 13: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling
Page 14: Our First Script - CoderDojo Athenry | CoderDojo … First Script Log in to Putty At the prompt type Then type Then press Ctrl + X Then press y followed by Enter To run it type Controlling